我有时间格式说:
$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。但它没有显示正确的结果。
我该如何处理这种时间减法?
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';
<?php
$d1 = strtotime('1:30 PM');
$d2 = strtotime('8:20 AM');
$diff = abs($d1-$d2);
echo "Difference is $diff secs";
?>
您将需要查看date()和strtotime()函数。
处理这种情况的方法是将日期转换为unix 时间戳,从另一个时间戳中减去一个时间戳,然后从结果时间戳中重新创建一个日期字符串。
注意:time()函数将返回当前系统时间(-stamp)