检查两次之间的时间 [英] Check time between two times

查看:35
本文介绍了检查两次之间的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想检查当前时间是否在 2 倍之间

All I want to check if current time is between 2 times

到目前为止,我浏览了多个脚本,但所有脚本都包含日期,或者太难了,或者脚本只计算完整的小时数,例如 6 或 7,而我需要检查小时和分钟

I browsed multiple scripts so far but all the scripts include also the date or is just to difficult, or the script counts only full hours like 6 or 7, while I need to check with hour and minutes

非常感谢您的帮助,我想要的脚本设置是

I would appreciate the help, the script setting I would like is

$begintime = 8:30;
$endtime = 17:00;

if(currenttime is between $begintime and $endtime) {
    // do something
} else {
    // do something else
}

推荐答案

在定义中使用 DateTime 对象.它将在内部使用当前日期,然后在比较时可以忽略.

Use DateTime object in the definition. It will internally use current date which can be then ignored in comparison.

$now = new DateTime();
$begin = new DateTime('8:00');
$end = new DateTime('17:00');

if ($now >= $begin && $now <= $end)

<小时>

高级:即使在执行过程中日期发生变化(DateTime 对象可能有 1 天的日期),为了防弹,请重新设置日期从 $now$start$end DateTime 对象到:


Advanced: for this to be bulletproof even when date changes during execution (DateTime objects can some have a 1-day-off date), re-set the date from the $now to the $start and $end DateTime object to:

$y = (int) $now->format('Y');
$m = (int) $now->format('n'); // n is the month without leading zeros
$d = (int) $now->format('d');

$begin->setDate($y, $m, $d);
$end->setDate($y, $m, $d);

或者,检查 Brick\DateTime 扩展,其中 <可以找到忽略日期的 code>LocalTime 类.

这篇关于检查两次之间的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆