English 中文(简体)
两时段之间的差别
原标题:Get Difference Between Two Times (Unix Epoch)

You know when it s late in the night and your brain is fried? I m having one of those nights right now, and my function so far is not working as it should, so please take a look at it: (I should note that I m using the PHP 5.2.9, and the function / method DateTime:Diff() is not available until PHP 5.3.0.

<?php
    function time_diff($ts1, $ts2) {
        # Find The Bigger Number
        if ($ts1 == $ts2) {
            return  0 Seconds ;
        } else if ($ts1 > $ts2) {
            $large = $ts1;
            $small = $ts2;
        } else {
            $small = $ts1;
            $large = $ts2;
        }
        # Get the Diffrence
        $diff = $large - $small;
        # Setup The Scope of Time
        $s = 1;         $ss = 0;
        $m = $s * 60;   $ms = 0;
        $h = $m * 60;   $hs = 0;
        $d = $h * 24;   $ds = 0;
        $n = $d * 31;   $ns = 0;
        $y = $n * 365;  $ys = 0;
        # Find the Scope
        while (($diff - $y) > 0) { $ys++; $diff -= $y; }
        while (($diff - $n) > 0) { $ms++; $diff -= $n; }
        while (($diff - $d) > 0) { $ds++; $diff -= $d; }
        while (($diff - $h) > 0) { $hs++; $diff -= $h; }
        while (($diff - $m) > 0) { $ms++; $diff -= $m; }
        while (($diff - $s) > 0) { $ss++; $diff -= $s; }
        # Print the Results
        return "$ys Years, $ns Months, $ds Days, $hs Hours, $ms Minutes & $ss Seconds.";
    }
    // Test the Function:
    ediff(strtotime( December 16, 1988 ), time());
    # Output Should be:
    # 20 Years, 11 Months, 8 Days, X Hours, Y Minutes & Z Seconds.
?>
最佳回答

如何做到这一点:

function time_diff($t1, $t2)
{
   $totalSeconds = abs($t1-$t2);
   $date = getdate($totalSeconds); 
   $firstYear = getdate(0);
   $years = $date[ year ]-$firstYear[ year ];
   $months = $date[ mon ];
   $days = $date[ mday ];
   $hours = $date[ hour ];
   $minutes = $date[ minutes ];
   $seconds = $date[ seconds ];

   return "$years Years, $months Months, $days Days, $hours Hours, $minutes Minutes & $seconds Seconds.";
}

这把特定时间的区别作为日期。 然后,你可以让“目标”为你们做一切工作。 唯一的挑战是年数——这只是年复一年(差异)减去“Unix epoch年”(1970年)。

如果你不喜欢使用一个实际月,你也可以将“年”时间按12个月的平均天数分开。

$months = $date[ yday ] / (365/12);

同样,在剩下的几天里,可以用模块组合列出。

$days = $date[ yday ] % (365/12);
问题回答

这是对你的问题的回答,但我只是想指出......。

while (($diff - $y) > 0) { $ys++; $diff -= $y; }

书写方式非常低

$ys = $diff / $y;
$diff = $diff % $y;

此外,

       else if ($ts1 > $ts2) {
                $large = $ts1;
                $small = $ts2;
        } else {
                $small = $ts1;
                $large = $ts2;
        }
        # Get the Diffrence
        $diff = $large - $small;

易于改写

$diff = abs($ts1 - $ts2);

我感到,如果问题不那么尖锐的话,你守则中的问题就会更加明显。

如何简单地简化第一部分

$diff = abs($ts2 - $ts1);

然后,当你这样做时:

 $n = $d * 31;   $ns = 0;
 $y = $n * 365;  $ys = 0;

实际上,你说,一年是365天,长31个月。 这实际上长达36年。 也许不是你想要的东西。

最后,我们都在这里成长。 请使用可变名称,即YEAR_IN_SECONDS而不是美元。 你可以清楚地看到,你可以一劳永逸地写法,但另外20个cks子将不得不阅读。

就特定时段内所有几个月的需要而言,我们利用了以下编码:

function MonthsBetweenTimeStamp($t1, $t2) {
   $monthsYear = array();
   $lastYearMonth  = strtotime(gmdate( F-Y , $t2));
   $startYearMonth = strtotime(gmdate( F-Y , $t1));
    while ($startYearMonth < $lastYearMonth) {
        $monthsYear[] = gmdate("F-Y", $startYearMonth);
                    //Increment of one month directly 
        $startYearMonth = strtotime(gmdate("F-Y", $startYearMonth) .   + 1 month );
    }

    if (empty($monthsYear)) {
        $monthsYear = array($startYearMonth));
    }

    return $monthsYear; 




相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...

热门标签