简体   繁体   English

两次相差(Unix时代)

[英]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 5.2.9,函数/ DateTime:Diff()方法直到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.
?>

This isn't an answer to your question, but I just wanted to point out... 这不是您问题的答案,但我只是想指出...

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

is a very inefficient way of writing 是一种非常低效的写作方式

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

Also, this 还有这个

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

can easily be rewritten as 可以很容易地重写为

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

I have a feeling that the problem in your code would be more apparent if it was less verbose. 我觉得,如果代码不那么冗长,您的代码中的问题将会更加明显。 :) :)

how about simplifying the first part with a simple 如何用简单的方法简化第一部分

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

Then, when you do this: 然后,当您执行此操作时:

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

you are actually saying that a year is composed of 365 31 day long months. 您实际上是说一年是由365个31天长的月份组成的。 which is actually about 36 year long years. 实际上大约是36年之久。 Probably not what you want. 可能不是您想要的。

Finally, we are all grown ups here. 最后,我们都是大人。 Please use grown up variable names ie $YEAR_IN_SECONDS instead of $ys. 请使用变长的变量名,即$ YEAR_IN_SECONDS而不是$ ys。 As you can clearly see, you may write code once, but 20 other schmucks are going to have to read it a lot of times. 正如您可以清楚地看到的那样,您可能只编写一次代码,但是其他20个schmucks将不得不多次阅读它。

In the case of needed all months during the given times-stamp then we have use of the following coding in php : 如果在给定的时间戳中需要所有月份,那么我们在php中使用以下代码:

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; 

How about this: 这个怎么样:

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.";
}

This uses the difference of the given times as a date. 这使用给定时间的差作为日期。 Then you can let the "getdate" do all the work for you. 然后,您可以让“ getdate”为您完成所有工作。 The only challenge is the number years - which is simply the getdate year (of the difference) minus the Unix epoch year (1970). 唯一的挑战是数字年份-简单来说就是(相差的)getdate年减去Unix纪元年(1970年)。

If you don't like using an actual month, you could also divide the "year" day by the number of days in 12 equal months 如果您不喜欢使用实际月份,也可以将“年”天除以12个相等月份中的天数

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

Similarly days could be figured out the remaining days with modulus 同样,可以用模数计算出剩余的天数

$days = $date['yday'] % (365/12);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM