【问题标题】:How can I check to see if a request is being made between certain hours on specific days?如何检查是否在特定日期的特定时间之间发出请求?
【发布时间】:2013-05-29 00:31:41
【问题描述】:

我想编写一个函数,如果请求是在星期一至星期六上午 8 点到下午 5 点之间发出的,则返回布尔值 true,其他时间返回 false。我知道我可能会使用date()strtotime(),但除此之外,我迷路了。有什么指点吗?

想要的结果

if (DuringBusinessHours()) {
    // execute this
} else {
   // execute this
}

【问题讨论】:

  • 你至少看过你提到的函数的文档吗?
  • @TimCooper 是的,我有。如果它像您的愤世嫉俗所暗示的那样简单,那么回答我的问题、获得赞成票并被标记为接受的答案不是更容易吗?
  • @anwyatt 那么您不清楚文档的哪一部分?
  • 嗨@anwyatt - 不要被这里偶尔简洁的语气吓到。受访者 - 包括我自己 - 经常对没有基础研究就提出问题的人数感到厌倦,因此很容易假设每个人都一样!在php 标签中,级别高得惊人,可能是因为它吸引了很多初学者。

标签: php


【解决方案1】:

我会建议这样的事情:

// create start and end date time
$start = new DateTime('8:00am');
$end = new DateTime('5:00pm');

// get the current time
$now = new DateTime();

// note that you can use the < > operators 
// to compare date time objects
if($now >= $start && $now < $end) {
    echo 'during business hours';
}

【讨论】:

    猜你喜欢
    • 2021-12-18
    • 2012-05-27
    • 2017-03-13
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    相关资源
    最近更新 更多