function getTimeRange($open,$closed) {
//$begin = new DateTime($open);
$begin = new DateTime($open);
$end = new DateTime($closed);
$interval = DateInterval::createFromDateString('30 min');
$times = new DatePeriod($begin, $interval, $end);
$timeRange = [];
foreach ($times as $time) {
$timeRange[] = $time->add($interval)->format('H:i');
}
return $timeRange;
}
它工作正常但有时我的开放时间是在夜晚。所以从10:00到3:00。我已经做到这一点,以便我的软件明白,如果商店营业至凌晨2点,则将日期设定为前一天...所以如果它的星期一15:00到2:00 ...凌晨2点仍然是星期一。< / p>
但是现在对于这个时间范围代码我也需要这样的东西,但我不知道如何处理这个问题。因为当你打开时:10:00关闭:23:59它工作正常但是当你把结束时间改为2点时它什么也没做。
如果结束时间如... 00:00到04:00我想制作一个范围并将其视为1天
// MAGUS AWNSER之后的更新 硬编码dit不起作用。但是在课堂上我删除了&#34;命名空间Carbon;&#34;在顶部..然后所有工作,你的代码也很棒!
但现在我得到了错误;
Warning: The use statement with non-compound name 'DateTime' has no effect in /Applications/XAMPP/xamppfiles/htdocs/r/classes/class.Carbon.php on line 14
Warning: The use statement with non-compound name 'DateTimeZone' has no effect in /Applications/XAMPP/xamppfiles/htdocs/r/classes/class.Carbon.php on line 15
Warning: The use statement with non-compound name 'InvalidArgumentException' has no effect in /Applications/XAMPP/xamppfiles/htdocs/r/classes/class.Carbon.php on line 16
我不知道这是什么意思...如果我也发表评论:
//namespace Carbon;
//use DateTime;
//use DateTimeZone;
//use InvalidArgumentException;
然后所有工作都没有错误...所以我不知道这是否会导致问题?
答案 0 :(得分:0)
我建议你用碳! https://github.com/briannesbitt/Carbon
你的功能看起来像这样:
<?php
function getTimeRange($open,$closed) {
$begin = Carbon::createFromFormat('H:i', $open); //assuming 20:00 here
$end = Carbon::createFromFormat('H:i', $closed); //assuming 04:00 here
//Checking if $closed is in the same day!
if(intval(explode(':',$open)[0]) > intval(explode(':',$closed)[0]){
//Truth means that 20 > 4, so $closed should be the next day!
$end->addDay();
}
//I didnt really understood what you wanted here but im guessing
//you want some sort of array like ['20:00','20:30','21:00',...]
$halfTimes = $begin->diffInHours($end) * 2;
$timeRange = [];
for($i=0;$i<$halfTimes;$i++){
$begin->addMinutes(30);
$timeRange[] = $begin->format('H:i');
}
return $timeRange;
}