我已经想了一个星期了。我妻子创办了一家新的出租车公司,她让我为这里编码一个简单的网页,她可以在那里按下按钮保存时间戳,然后在她下班时再次按下,然后创建第二个时间戳
我有一个MYSQL数据库,其中的行包含开始时间和停止时间。我已经设法使用diff函数来查看两个时间戳之间的时间,但现在是棘手的部分。
由于一天中不同时间的付款不同,我需要缩短时间。
直到19:00,她"白天"工作,然后"晚上"工作到前几天06:00,然后还有"周末白天"one_answers"周末晚上"。
因此,如果她创建了一个包含日期和时间的时间戳:2018-08-08 06:30,然后在2018-08-0821:00创建另一个时间戳,那么我需要一个脚本,将这些数据放在前"$daytimehours=12"$daytime minutes=30"one_answers"$nighttimehours=3"$nihtimeminutes=0"中
我已经设法创建了一个几乎可以工作的脚本,但它有好几页长,并且它包含了一个if语句,用于每个不同的场景——白天、晚上、白天等等。
那么,有人知道如何解决这个问题吗?或者只是给我指一个正确的方向。我很乐意花点钱让它发挥作用。
我的解决方案是
<?php
date_default_timezone_set('Asia/Almaty');
$endDate = '2018-08-08 21:00';
$startDate = '2018-08-08 06:30';
$nightmare = date('Y-m-d 19:00');
$startDay = date('Y-m-d 06:00');
$diffMorning = strtotime($nightmare) - strtotime($startDate);
$diffNight = strtotime($endDate) - strtotime($nightmare);
echo gmdate('H:i', $diffMorning) . "n"; // this is the difference from start day till 19:00
echo gmdate('H:i', $diffNight); // this is the difference on nightmare
$total = $diffMorning + $diffNight;
echo intval($total/3600) . " hours n";
echo $total%3600/60 . " minutes n";
echo $total%3600%60 . ' seconds';
您可以通过在线编译器进行检查
给定两个日期,表示为:
$endDate = '2018-08-08 21:00';
$startDate = '2018-08-08 06:30';
您可以使用PHP日期扩展来实现如下差异:
$start = date_create($startDate);
$end = date_create($endDate);
$boundnight = clone($end);
$boundnight->setTime(19,00);
$total_duration = date_diff($end,$start);//total duration from start to end
$day_duration = date_diff($boundnight,$start);//daytime duration
$night_duration = date_diff($end,$boundnight);// nighttime duration
您可以使用格式化方法以这种方式打印人类可读的字符串:
$total_duration=$total_duration->format('%H:%I');
$day_duration=$day_duration->format('%H:%I');
$night_duration=$night_duration->format('%H:%I');
在这一步中,除了你说你想将每个持续时间转换为分钟外,什么都没有了。因此,让我们构建一个函数:
function toMinute($duration){
return (count($x=explode(':',$duration))==2?($x[0]*60+$x[1]):false);
}
然后你可以这样使用它:
$total_duration = toMinute($total_duration);
$day_duration = toMinute($day_duration);
$night_duration = toMinute($night_duration);
的输出该步骤的var_dump($total_duration,$day_duration,$night_duration)
为:
int(870)
int(750)
int(120)