我应该将函数的结果存储到数组中吗? [英] Should I store the result of an function into an array?

查看:175
本文介绍了我应该将函数的结果存储到数组中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的功能:

  function time_elapsed_string($ ptime)
{
$ date_time = strtotime(1348-10-10 04:30:01)+ $ ptime;
$ year = date(Y,$ date_time);
$ month = date(m,$ date_time);
$ day = date(d,$ date_time);
$ time = date(H:i:s,$ date_time);

$ etime = time() - $ ptime + 1;

$ a = array(31536000 =>'year',
2592000 =>'month',
86400 =>'day',
3600 ='小时',
60 =>'分钟',
1 =>'second'
);

foreach($ a as $ secs => $ str)
{
$ d = $ etime / $ secs;
if($ d> = 1)
{
$ r = round($ d);
// EX:
return array('date'=> $ day .'-'。$ month .'-'。$ year,// 2016-02-20
' time'=> $ time,// 03:30:04
'difference'=> $ r。''。$ str。'ago'// 2个月前
);
}
}
}

我这样使用它:

  $ ptime = 1470692661; 
$ html ='< span title =date:'.time_elapsed_string($ ptime)['date']。'time:'.time_elapsed_string($ ptime)['time']。'> in .time_elapsed_string($的ptime)[ '差'] '<跨度>';

正如您所看到的,我正在使用该函数的结果,如下所示:


$ b $

  time_elapsed_string($ ptime)['date'] 
ime_elapsed_string($ ptime)['time']
time_elapsed_string($ ptime )['difference']

事实上,我每次需要调用该函数结果。是对的吗?或者我应该调用它并将其存储到数组中?



注意:我的代码也可以。

解决方案

DateTime 自PHP 5.2.0以来一直可用,人们低估了它。为什么不使用这个而不是 loop s和 if s?

  $ create_time =2016-08-02 12:35:04; 
$ current_time =2016-08-02 16:16:02;

$ dtCurrent = DateTime :: createFromFormat('Y-m-d H:i:s',$ current_time);
//使用当前时间戳记,使用以下内容:
// $ dtCurrent = new DateTime();
$ dtCreate = DateTime :: createFromFormat('Y-m-d H:i:s',$ create_time);
$ diff = $ dtCurrent-> diff($ dtCreate);

现在,您可以

  $ interval = datetime.format.phprel =nofollow>格式结果, $ diff->格式(%h小时%i分钟%s秒); 

这会给出一个干净的 3小时40分58秒没有任何数组,这是更好的。



更新

是通过正则表达式获得小时/分钟/秒的一般解决方案:

  $ interval = $ diff-> format( y年%m月%d日%h小时%i分钟%s秒); 

//现在删除零值
$ interval = preg_replace('/(^ 0 | 0)(年|月|天|小时|分钟|秒)/','', $间隔);

更新2



至于你的评论:


看,我想用你的方法..但我真的不能实现它..其实我需要三件事:时间日期差异 .. !但是你的方法并没有给我它们。

好吧,我们已经知道如何获得差异,它是<$ c

要获取时间和日期,可以从 $ dtCreate 变量,再次使用格式:

  $ time = $ dtCreate-> format 'H:I:S'); 
$ date = $ dtCreate->格式('d-m-Y');


I have a function like this:

function time_elapsed_string($ptime)
{
            $date_time = strtotime("1348-10-10 04:30:01") + $ptime;
            $year = date("Y",$date_time);
            $month = date("m",$date_time);
            $day = date("d",$date_time);
            $time = date("H:i:s",$date_time);

    $etime = time() - $ptime + 1;

    $a = array( 31536000  =>  'year',
                 2592000  =>  'month',
                   86400  =>  'day',
                    3600  =>  'hour',
                      60  =>  'minute',
                       1  =>  'second'
                );

    foreach ($a as $secs => $str)
    {
        $d = $etime / $secs;
        if ($d >= 1)
        {
            $r = round($d);
                                                                   // EX:
            return array('date' => $day.'-'.$month.'-'.$year,      // 2016-02-20
                         'time' => $time,                          // 03:30:04
                         'difference' => $r . ' ' . $str . ' ago'  // 2 month ago
                        );
        }
    }
}

And I use it like this:

$ptime = 1470692661;
$html = '<span title="date: '.time_elapsed_string($ptime)['date'].' time: '.time_elapsed_string($ptime)['time'].'">in '.time_elapsed_string($ptime)['difference'].'<span>';

As you see, I'm using of that function's result like this:

time_elapsed_string($ptime)['date']
ime_elapsed_string($ptime)['time']
time_elapsed_string($ptime)['difference']

In fact I'm calling that function every time I need one of its results. Is that right? Or should I call it once and store it into an array?

Note: My code works as well.

解决方案

Counting time elapsed since some date/time like this is mauvais ton.

DateTime has been available since PHP 5.2.0 and tonns of people underestimate it. Why don't you use this instead of loops and ifs?

$create_time = "2016-08-02 12:35:04";
$current_time="2016-08-02 16:16:02";

$dtCurrent = DateTime::createFromFormat('Y-m-d H:i:s', $current_time);
// to use current timestamp, use the following:
//$dtCurrent = new DateTime();
$dtCreate = DateTime::createFromFormat('Y-m-d H:i:s', $create_time);
$diff = $dtCurrent->diff($dtCreate);

Now, you can format the result however you want:

$interval = $diff->format("%h hours %i minutes %s seconds");

This will give a clean 3 hours 40 minutes 58 seconds without any arrays, which is better.

UPDATE

There is a general solution to get hours / minutes / seconds via regex:

$interval = $diff->format("%y years %m months %d days %h hours %i minutes %s seconds");

// now remove zero values
$interval = preg_replace('/(^0| 0) (years|months|days|hours|minutes|seconds)/', '', $interval);

UPDATE 2

As of your comment:

Look, I want to use your approach .. but I really cannot implement it .. Actually I need three things: time, date, difference ..! But your approach doesn't give me them..

Well, we already know how to get the difference, it's the $interval variable described above.

To get time and date, you can get it from the $dtCreate variable by, again, using format:

$time = $dtCreate->format('H:i:s');
$date = $dtCreate->format('d-m-Y');

这篇关于我应该将函数的结果存储到数组中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆