我需要一些帮助来计算当前的分钟数:在足球比赛中进行的秒数,让我们假设足球比赛有:
$schedule='2014-02-13';
$start_time1='03:00 pm';
$end_time1='03:45 pm';
$start_time2='04:00 pm';
$end_time2='04:50 pm';
$start_extra_time1='05:10 pm';
$end_extra_time1='05:30 pm';
$start_extra_time2='05:35 pm';
$end_extra_time2='06:10 pm';
而实际上我现在的时间是“下午05:32”,我如何计算比赛的当前比赛时间:秒或(小时:分钟:秒)?
我尝试了许多解决方案,比如datediff,strtotime等......但没有运气,因为这些日期之间有一段关闭时间。
非常感谢你的帮助。
答案 0 :(得分:0)
以下是使用DateTime()
和DateInterval()
:
<?php
$schedule='2014-02-13';
$start_time1='03:00 pm';
$end_time1='03:45 pm';
$start_time2='04:00 pm';
$end_time2='04:50 pm';
$start_extra_time1='05:10 pm';
$end_extra_time1='05:30 pm';
$start_extra_time2='05:35 pm';
$end_extra_time2='06:10 pm';
$start1 = new DateTime($start_time1);
$end1 = new DateTime($end_time1);
$firstInterval = $start1->diff($end1);
$start2 = new DateTime($start_time2);
$end2 = new DateTime($end_time2);
$secondInterval = $start2->diff($end2);
$start3 = new DateTime($start_extra_time1);
$end3 = new DateTime($end_extra_time1);
$thirdInterval = $start3->diff($end3);
$start4 = new DateTime($start_extra_time2);
$end4 = new DateTime($end_extra_time2);
$fourthInterval = $start4->diff($end4);
$baseline = new DateTime();
$accrual = clone $baseline;
$accrual->add($firstInterval);
$accrual->add($secondInterval);
$accrual->add($thirdInterval);
$accrual->add($fourthInterval);
$difference = $accrual->diff($baseline);
echo $difference->format('%h hours, %i minutes');
所有这些代码都是:
创建表示每个时间间隔的开始和结束时间的DateTime()
个对象。
获取一个DateInterval()
对象,表示两者之间的差异。
创建基线(起点)DateTime()
对象
创建一个DateTime()
对象,我们添加间隔以表示已用时间
回应两个
如果您使用PHP 5.4或更高版本,可以缩短部分内容:
$firstInterval = (new DateTime($start_time1))->diff((new DateTime($end_time1)));
$secondInterval = (new DateTime($start_time2))->diff((new DateTime($end_time2)));
$thirdInterval = (new DateTime($start_extra_time1))->diff((new DateTime($end_extra_time1)));
$fourthInterval = (new DateTime($start_extra_time2))->diff((new DateTime($end_extra_time2)));
和
$accrual->add($firstInterval)->add($secondInterval)->add($thirdInterval)->add($fourthInterval);
答案 1 :(得分:0)
我添加了John的解决方案,这是我的代码:
<?php
date_default_timezone_set("Canada/Eastern");
$date="2014-02-14 14:00:00 +00";
$start_time1='09:00 am';
$end_time1='09:45 am';
$start_time2='10:00 am';
$end_time2='10:50 am';
$start_extra_time1='';
$end_extra_time1='';
$start_extra_time2='';
$end_extra_time2='';
$start1 = new DateTime($start_time1);
$end1 = new DateTime($end_time1);
$firstInterval = $start1->diff($end1);
$start2 = new DateTime($start_time2);
$end2 = new DateTime($end_time2);
$secondInterval = $start2->diff($end2);
$start3 = new DateTime($start_extra_time1);
$end3 = new DateTime($end_extra_time1);
$thirdInterval = $start3->diff($end3);
$start4 = new DateTime($start_extra_time2);
$end4 = new DateTime($end_extra_time2);
$fourthInterval = $start4->diff($end4);
$baseline = new DateTime($date);
$baseline->setTimezone(new DateTimeZone('Canada/Eastern')); // -05
$accrual = new DateTime();
$accrual->add($firstInterval);
$accrual->add($secondInterval);
$accrual->add($thirdInterval);
$accrual->add($fourthInterval);
$difference = $accrual->diff($baseline);
echo "baseline time :\n";
var_dump($baseline);
echo "accrual time :\n";
var_dump($accrual);
print json_encode(array('response'=>$difference->format('%h hours, %i minutes, %s seconds')));
?>
游戏尚未开始,但它在游戏中显示1小时,这怎么可能? http://easycaptures.com/fs/uploaded/645/4891434894.png
答案 2 :(得分:-1)
Mysql有 timediff 的错误,所以请使用:
Time_To_Sec($currentime) - Time_To_Sec($end_time2) As time_seconds
同时检查您的时间格式,避免使用字符串作为时间或日期,格式化为日期/时间实体。