php 后台配置时间段前端应用解析并判断时间段是否在这时间段之内
有时候我们需要在后台配置某个时间段做什么事情,那么人性化的配置一般是: 21:30-8:30 这样的配置,那么我们使用的时候怎么来判断当前的时间是否在这个时间段内呢
下面是我的一段参考代码,仅作参考:
$mp_lyEnable = '21-8:30'; //后台设置的时间段
$lyEnable = false; //客服是否在离线时段
if ($mp_lyEnable) {
$mp_lyEnable = explode('-', $mp_lyEnable);
if (count($mp_lyEnable) == 2) {
@list($startHour, $startMinute) = explode(':', $mp_lyEnable[0]);
@list($endHour, $endMinute) = explode(':', $mp_lyEnable[1]);
if (is_null($startMinute)) $startMinute = 0;
if (is_null($endMinute)) $endMinute = 59;
$startTime = strtotime(sprintf(date('Y-m-d %\d:%\d:00'), $startHour, $startMinute));
$endTime = strtotime(sprintf(date('Y-m-d %\d:%\d:00'), $endHour, $endMinute));
if ($endHour < $startHour) {
//跨天
$endTime += 86400;
}
$now = time();
if ($now > $startTime && $now < $endTime) $lyEnable = true;
var_dump(date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', $endTime));
}
}
版权属于:Joyber
本文链接:https://blog.qqvbc.com/default/349.html
转载时须注明出处及本声明