如何在给定工作日列表的情况下找到最早的即将到来的日期?
PHP 中的日期对我来说是一场噩梦,所以请帮助我的其他编码人员...我想通知客户他们的订单将在哪一天交付。它的工作原理如下:
我有 2 个运输区域,A 和 A。 B. A区订单每周一、三、四发货。星期五,而 B 区是星期二、星期四、星期六。对于每个订单,交货日安排在下一个可用日,具体取决于区域。请考虑,如果有人在星期一下订单,货物将在下一个可用日期交货,即 B 区为星期二,A 区为星期三。
我如何计算下一个可用交货日期并通知客户?
谢谢。
Dates in PHP are a nightmare for me so please help me out fellow coders... I want to notify customers about the day their order will be delivered. It works like this:
I have 2 shipping zones, A & B. Orders for zone A are delivered each Monday, Wednesday & Friday, whereas zone B is on Tuesday, Thursday, Saturday. For each order, the delivery day is scheduled for the NEXT AVAILABLE day, depending on the zone. Please consider that if someone places an order on Monday the goods will be delivered on the NEXT available date, that would be Tuesday for zone B and Wednesday for zone A.
How can I calculate the NEXT AVAILABLE delivery date and notify the customer?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这当然不是最快或最聪明的答案,但阅读代码将是一种乐趣。
假设我们在 A 区发货:
您可能需要查看 PHP 中提供的相对日期格式,这就是“下周一”魔法发生的部分。有关如何使用
$shippingDate
的信息,请参阅类日期时间
。更新
为了完整起见,这里是一个更老式的版本,不需要 PHP 5.3 并且也应该更快(尽管速度实际上与这里无关)。我不太喜欢它,因为验证它是否正常工作并不容易。与上面的版本相比,这个版本在我第一次编写时有一个错误。简单就是好的。
查看实际操作。
This will certainly not be the fastest or most clever answer, but it's going to be a pleasure to read the code.
Assuming we are shipping in zone A:
You might want to take a look at the relative date formats available in PHP, this is the part where the "next monday" magic happens. For information on what you can do with
$shippingDate
, see the documentation on classDateTime
.Update
For completeness, here is a more old-school version which does not need PHP 5.3 and should also be faster (although speed is practically irrelevant here). I don't like it as much, because it's not easy to verify that it works correctly. In contrast to the version above, this one had a bug when I first wrote it. Simple is good.
See it in action.
试试这个:
Try this: