我试图计算当前时间与企业重新开业之间的时间跨度(以小时为单位)。
当一个企业已经关闭了一天,我被困在第二天。有没有简单的方法来做到这一点?
$storeSchedule = [
'Mon' => ['09:00 AM' => '12:00 AM'],
'Tue' => ['09:00 AM' => '12:00 AM'],
'Wed' => ['09:00 AM' => '12:00 AM'],
'Thu' => ['09:00 AM' => '12:00 AM'],
'Fri' => ['09:00 AM' => '12:00 AM'],
'Sat' => ['12:00 AM' => '01:00 AM', '09:00 AM' => '12:00 AM']
];
// current (or inputted time)
$timestamp = time();
$status = -1;
$currentTime = (new DateTime())->setTimestamp($timestamp);
foreach ($storeSchedule[date('D', $timestamp)] as $startTime => $endTime) {
// create time objects from start/end times
$startTime = DateTime::createFromFormat('h:i A', $startTime);
$endTime = DateTime::createFromFormat('h:i A', $endTime);
// Business will open later today
if ($startTime > $currentTime) {
$status = round(abs($currentTime - $startTime) / 3600,2);
break;
}
// Business is open right now
if (($startTime < $currentTime) && ($currentTime < $endTime)) {
$status = 0;
break;
}
}
echo $status;
1条答案
按热度按时间xv8emn3q1#
这是一个工作的例子,看看它是否适合你。