简体   繁体   English

两个时间戳之间的时间计算

[英]Time calculation between two timestamps

I have two timestamps provided by time() and stored in database as varchar. 我有两个time()提供的时间戳,并以varchar的形式存储在数据库中。

Now I want to calculate the difference between two times; 现在我想计算两次之间的差; one is 1366627990 , and the other one $time2=time(); 一个是1366627990 ,另一个是$time2=time(); .

I used a script 我用了一个剧本

function dateDiff($time1, $time2, $precision = 3) {
        date_default_timezone_set("UTC");
    // If not numeric then convert texts to unix timestamps
    if (!is_int($time1)) {
      $time1 = strtotime($time1);
    }
    if (!is_int($time2)) {
      $time2 = strtotime($time2);
    }

    // If time1 is bigger than time2
    // Then swap time1 and time2
    if ($time1 > $time2) {
      $ttime = $time1;
      $time1 = $time2;
      $time2 = $ttime;
    }

    // Set up intervals and diffs arrays
    $intervals = array('year','month','day','hour','minute','second');
    $diffs = array();

    // Loop thru all intervals
    foreach ($intervals as $interval) {
      // Set default diff to 0
      $diffs[$interval] = 0;
      // Create temp time from time1 and interval
      $ttime = strtotime("+1 " . $interval, $time1);
      // Loop until temp time is smaller than time2
      while ($time2 >= $ttime) {
    $time1 = $ttime;
    $diffs[$interval]++;
    // Create new temp time from time1 and interval
    $ttime = strtotime("+1 " . $interval, $time1);
      }
    }

    $count = 0;
    $times = array();
    // Loop thru all diffs
    foreach ($diffs as $interval => $value) {
      // Break if we have needed precission
      if ($count >= $precision) {
    break;
      }
      // Add value and interval 
      // if value is bigger than 0
      if ($value > 0) {
    // Add s if value is not 1
    if ($value != 1) {
      $interval .= "s";
    }
    // Add value and interval to times array
    $times[] = $value . " " . $interval;
    $count++;
      }
    }

    // Return string with times
    return implode(", ", $times);
  }

and I call the function as 我将函数称为

echo dateDiff("1366627990",$time2, 3);

but it's giving me 但这给了我

43 years, 3 months, 22 days

while the time should not exceed maximum of 10 days . 时间最多不能超过10 days

Is there something wrong with my script? 我的脚本有问题吗?

Well, first just grab the intval() of the two timestamps, subtract one from the other and take the absolute value of the result. 好吧,首先只需抓住两个时间戳的intval(),从另一个时间戳中减去一个,然后取结果的绝对值。 You'll end up with the total number of seconds between the two times. 您将得到两次之间的总秒数。

Then use one of these examples to convert that number (of seconds) to something human readable: 然后使用以下示例之一将该数字(秒)转换为人类可读的内容:

seconds to minutes and days to weeks 秒到几分钟,几天到几周

First of all, don't expect the difference to be less than 10 days. 首先,不要指望差异少于10天。 It's 31 years: 这是31年:

echo date("r", 366627990); // your first time. Fri, 14 Aug 1981 09:06:30 +0000
echo date("r", 1366971706); // $time2 in my case. Fri, 26 Apr 2013 10:21:46 +0000

Update: 更新:

Okay, now you've corrected the date to 1366627990 . 好的,现在您已将日期更正为1366627990 That's a difference of 4 days: 相差4天:

echo date("r", 1366627990); // updated time. Mon, 22 Apr 2013 10:53:10 +0000

But the solutions provided in this answer are still correct and are valid for your old and new timestamp (as well as for any other two timestamps :-) ). 但是此答案中提供的解决方案仍然是正确的,并且对于您的旧时间戳和新时间戳(以及任何其他两个时间戳:-)都是有效的。

Now let me get to the problem in your script and then show you a much better alternative for calculating time and date differences. 现在,让我解决您脚本中的问题,然后向您展示一种更好的替代方法来计算时间和日期差。

Fixing your script 修正脚本

The problem is your "If not numeric then convert texts to unix timestamps" check in the beginning. 问题是您的“如果不是数字则将文本转换为unix时间戳”在开始时进行检查。 Because this won't work if you pass UNIX timestamps as strings, which you apparently do. 因为如果您将UNIX时间戳作为字符串传递,那么这样做将不起作用,您显然可以这样做。 A hack that fixes this would be to include the following at the beginning of the function: 要解决此问题,可以在该功能的开头添加以下内容:

function dateDiff($time1, $time2, $precision = 3) {
    // hack for passing timestamps as strings
    if ($time1 == intval($time1)) {
        $time1 = intval($time1);
    }
    if ($time2 == intval($time2)) {
        $time2 = intval($time2);
    }

Now, your function yields the correct output: 现在,您的函数将产生正确的输出:

31 years, 8 months, 12 days

A better way for calculating time intervals 计算时间间隔的更好方法

As of version 5.2.0, PHP has the super-neat DateTime class with a diff method , which is exactly what you need: 从5.2.0版开始,PHP具有带有diff方法的超净DateTime ,这正是您所需要的:

$d1 = new DateTime();
$d1->SetTimestamp($time1);
$d2 = new DateTime();

$diff = $d1->diff($d2);

echo $diff->format("%y years, %m months, %d days");

This one also gives you 这个也给你

31 years, 8 months, 12 days

Try 尝试

<?php
    $time1 = (int)1366971673; //The value of time() when I wrote this.
    $time2 = (int)1366627990; //Your time value to compare.

    $diff = (int)$time1-$time2;

    echo round(($diff/60/60/24)).' days between dates.';
?>

Should output; 应该输出;

4 days between dates.

http://phpfiddle.org/main/code/ahc-mg3 http://phpfiddle.org/main/code/ahc-mg3

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

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