决定营业时间开放/关闭

编程入门 行业动态 更新时间:2024-10-26 20:30:06
本文介绍了决定营业时间开放/关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果时间是AM到PM(上午11点到下午10点),我的代码工作正常,但是如果操作时间为AM到AM(例如上午9点 - 凌晨1点),则它的代码工作正常。这是我的代码:

My code works fine if the times are AM to PM (Ex: 11 AM - 10 PM), but if the locations hours of operation are AM to AM (Ex: 9 AM - 1 AM) it breaks. Here is my code:

$datedivide = explode(" - ", $day['hours']); //$day['hours'] Example 11:00 AM - 10:00 PM $from = ''.$day['days'].' '.$datedivide[0].''; $to = ''.$day['days'].' '.$datedivide[1].''; $date = date('l g:i A'); $date = is_int($date) ? $date : strtotime($date); $from = is_int($from) ? $from : strtotime($from); $to = is_int($to) ? $to : strtotime($to); if (($date > $from) && ($date < $to) && ($dateh != 'Closed')) { ?> <script type="text/javascript"> $(document).ready(function(){ $('.entry-title-container').append('<div class="column two"><h2 style="color:green;text-align: left;margin: 0;">OPEN<br /><span style="color:#222;font-size:12px;display: block;">Closes at <?php echo $datedivide[1]; ?></span></h2></div><br clear="all" />'); }); </script> <?php }

推荐答案

p>您首先需要创建一个数组,这个数组将保留一周的日期,以及各自的关闭/打开时间范围。

You would first need to create an array which will hold your days of the week, and their respective close/open time range(s).

/** * I setup the hours for each day if they carry-over) * everyday is open from 09:00 AM - 12:00 AM * Sun/Sat open extra from 12:00 AM - 01:00 AM */ $storeSchedule = [ 'Sun' => ['12:00 AM' => '01:00 AM', '09:00 AM' => '12:00 AM'], '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'] ];

然后,循环当前的时间范围,并检查当前时间或提供的时间戳在一个范围内。您可以使用 DateTime 类来生成日期时间

You then loop over the current day's time range(s) and check to see if the current time or supplied timestamp is within a range. You do this by using the DateTime class to generate a DateTime object for each time range's start/end time.

以下将执行此操作,允许您指定时间戳,以防您希望检查提供的时间戳时间戳而不是当前时间。

The below will do this and allow you to specify a timestamp in case you are wanting to check a supplied timestamp instead of the current time.

// current or user supplied UNIX timestamp $timestamp = time(); // default status $status = 'closed'; // get current time object $currentTime = (new DateTime())->setTimestamp($timestamp); // loop through time ranges for current day 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); // check if current time is within a range if (($startTime < $currentTime) && ($currentTime < $endTime)) { $status = 'open'; break; } } echo "We are currently: $status";

请参阅 DEMO

See DEMO of above

更多推荐

决定营业时间开放/关闭

本文发布于:2023-10-23 01:39:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:营业时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!