2

我的数据库中有 4 列,它们是:

Date_in | Time_in | Date_out | Time_out

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';

但它不起作用。我认为我的引号和双引号都搞砸了,因为使用" ' .真的让我感到困惑..

提前感谢您提供有关如何修复代码的任何建议!

[编辑]感谢大家的所有详细帮助!我刚刚意识到服务器不支持 php 5.3,我不能使用 datetime()。

所以我的解决方案是:

$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);
4

5 回答 5

7

试试这个,

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

或者

您可以使用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 ";

编辑:

结帐链接,

如何使用 PHP 计算两个日期之间的差异?

Php 日期时间 – 计算 2 个日期之间差异的 7 种方法。

可以帮助你。

于 2013-02-18T14:18:54.347 回答
2

如果要评估变量内容,则不需要引号。在这种情况下,您只需要它们来创建日期和时间之间的分隔:

$start_time = new DateTime($list['date_in']." ".$list['time_in']);
$since_start = $start_time->diff(new DateTime($list['date_out']." ".$list['time_out']));

我使用点.来将变量与字符串连接起来,在这种情况下,字符串是一个空格。

您可以在文档中阅读有关串联的更多信息。

于 2013-02-18T14:15:56.700 回答
1
$start_time = new DateTime("$list[date_in] " . "$list[time_in]");
$since_start = $start_time->diff(new DateTime("$list[date_out] " . "$list[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']}"));
于 2013-02-18T14:15:49.870 回答
1
// 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);

最后一个参数是精度。

于 2013-02-18T14:22:35.293 回答
0
$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 ));
    }
}
于 2014-04-22T09:35:32.693 回答