php:datetime() 2 个日期时间与 2 个变量之间的差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14938339/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php: datetime() difference between 2 datetime with 2 variables
提问by Cuppy
I have in my database 4 columns which are:
我的数据库中有 4 列,它们是:
Date_in | Time_in | Date_out | Time_out
Date_in and Time_in belong together (e.g., 2013-02-18 13:00:00) and date_out goes with Time_out. I would like to find out the difference between and I have gotten up till:
Date_in 和 Time_in 属于一起(例如,2013-02-18 13:00:00)并且 date_out 与 Time_out 一起。我想找出和我已经起床的区别:
$start_time = new DateTime("'$list[date_in] "."$list[time_in]'");
$since_start = $start_time->diff(new DateTime("'$list[date_out] "."$list[time_out]'"));
$hours = $since_start->h.' hours';
But it doesn't work. I think my quotes and double quotes are all messed up because the use of " ' .really confuses me..
但它不起作用。我认为我的引号和双引号都搞砸了,因为使用" ' 。真的让我感到困惑..
Thanks in advance for any advice on how I can fix my code!
预先感谢您提供有关如何修复代码的任何建议!
[EDIT] Thanks everyone for all your detailed help! I just realised that the server doesn't support php 5.3 and I can't use datetime().
[编辑]感谢大家的详细帮助!我刚刚意识到服务器不支持 php 5.3,我不能使用 datetime()。
So my solution was:
所以我的解决方案是:
$start_time = strtotime("$list[date_in] " . "$list[time_in]");
$end_time = strtotime("$list[date_out] " . "$list[time_out]");
$hours = abs(($end_time - $start_time)/3600);
回答by Tony Stark
Try this,
尝试这个,
function datediff( $date1, $date2 )
{
    $diff = abs( strtotime( $date1 ) - strtotime( $date2 ) );
    return sprintf
    (
        "%d Days, %d Hours, %d Mins, %d Seconds",
        intval( $diff / 86400 ),
        intval( ( $diff % 86400 ) / 3600),
        intval( ( $diff / 60 ) % 60 ),
        intval( $diff % 60 )
    );
}
print datediff( "18th February 2013", "now" ) . "\n";
OR
或者
You can use DateTime::diff
您可以使用DateTime::diff
  $start_date = new DateTime("2012-02-10 11:26:00");
    $end_date = new DateTime("2012-04-25 01:50:00");
    $interval = $start_date->diff($end_date);
    echo "Result " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";
EDIT:
编辑:
Checkout links,
结帐链接,
How to calculate the difference between two dates using PHP?
Php Date Time – 7 Methods to Calculate the Difference between 2 dates.
Php 日期时间 - 计算 2 个日期之间差异的 7 种方法。
may help you.
可能会帮助你。
回答by Alvaro
You don't need quotes if you want to evaluate the variables content. In this case, you only need them to create a separation between date and time:
如果要评估变量内容,则不需要引号。在这种情况下,您只需要它们来创建日期和时间之间的分隔:
$start_time = new DateTime($list['date_in']." ".$list['time_in']);
$since_start = $start_time->diff(new DateTime($list['date_out']." ".$list['time_out']));
I am using the dot .in order to concatenate variables with an string, in this case the string is a white space.
我使用点.来将变量与字符串连接起来,在这种情况下,字符串是一个空格。
You can read more about concatenation in the documentation.
您可以在文档中阅读有关串联的更多信息。
回答by vikingmaster
$start_time = new DateTime("$list[date_in] " . "$list[time_in]");
$since_start = $start_time->diff(new DateTime("$list[date_out] " . "$list[time_out]"));
OR
或者
$start_time = new DateTime("{$list['date_in']} {$list['time_in']}");
$since_start = $start_time->diff(new DateTime("{$list['date_out']} {$list['time_out']}"));
回答by fnkr
// Time format is UNIX timestamp or
// PHP strtotime compatible strings
function dateDiff($time1, $time2, $precision = 6) {
    // 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);
}
// Set start & end time
$start_time = "2013-02-18 13:00:00";
$end_time = "2013-02-16 10:00:00";
// Run and print diff
echo dateDiff($start_time, $end_time, 6);
The last argument is the precision.
最后一个参数是精度。
回答by Naresh Chennuri
$diff = abs( strtotime( '2014-04-25 16:00:00' ) - strtotime( '2014-04-27 18:02:00' ) );
if(sprintf("%d",intval( $diff / 86400 )) != '0'){
    if(sprintf("%d",intval( $diff / 86400 )) == '1'){
        echo sprintf("%02d day ", intval( $diff / 86400 ));
    }else{
        echo sprintf("%02d days ", intval( $diff / 86400 ));
    }
}
if(intval( ( $diff % 86400 ) / 3600) != '0'){
    if(intval( ( $diff % 86400 ) / 3600) == '1'){
        echo sprintf("%02d hour ", intval( ( $diff % 86400 ) / 3600));
    }else{
        echo sprintf("%02d hours ", intval( ( $diff % 86400 ) / 3600));
    }
}
if(intval( ( $diff / 60 ) % 60 ) != '0'){
    if(intval( ( $diff / 60 ) % 60 ) == '1'){
        echo sprintf("%02d min", intval( ( $diff / 60 ) % 60 ));
    }else{
        echo sprintf("%02d mins", intval( ( $diff / 60 ) % 60 ));
    }
}

