1

我有时间格式说:

$start_time; (example: 1:30 PM)
$system_time; (example: 8:20 AM)        
echo $time_left_for_discussion = $start_time - $system_time;

我想减去 $start_time - $system_time。但它没有显示正确的结果。

我该如何处理这种时间减法?

4

4 回答 4

2

DateTime::createFromFormat()两次都使用,然后减去那些。

$TimeStart = DateTime::createFromFormat( 'g:i A', $start_time );
$TimeEnd = DateTime::createFromFormat( 'g:i A', $end_time );

$Interval = $TimeStart->diff( $TimeEnd );
$time_left = $Interval->h . 'h' . $Interval->m 'm';
于 2013-03-28T08:33:33.213 回答
2
<?php
  $d1 = strtotime('1:30 PM');
  $d2 = strtotime('8:20 AM');
   $diff = abs($d1-$d2);

  echo "Difference is $diff secs";

?>
于 2013-03-28T08:38:25.163 回答
1

您将需要查看date()strtotime()函数。

处理这种情况的方法是将日期转换为unix 时间戳,从另一个时间戳中减去一个时间戳,然后从结果时间戳中重新创建一个日期字符串。

注意:time()函数将返回当前系统时间(-stamp)

于 2013-03-28T08:32:57.393 回答
1

我可以建议你看看这个http://php.net/manual/en/function.strtotime.php

这是String to time,它非常有用。

祝你有美好的一天:D

于 2013-03-28T08:33:06.527 回答