0

我需要每周在特定的时间窗口中运行代码,并且如果我们在该窗口之外运行的等效代码。它需要智能夏令时。

该窗口在每周四都柏林时区时间 19:55 到 21:05 之间,如果在该时间段内运行 functionX(),否则运行 functionY()。

date_default_timezone_set("Europe/Dublin");
$currentDay = date("N");
$currentTime = date("H:i"); 

if (($currentDay == 4) && (($currentTime >= strtotime("19:55:00") ) && ($currentTime <= strtotime("21:05:00") ) ) ) {
    functionX();
} else {
    functionY();
}

我是否在正确的轨道上,有没有更好的方法来实现这个逻辑?

4

2 回答 2

0

哦,在括号问题旁边,您将$currentTime(常规字符串,date)与整数(Unix时间戳,strtotime)进行比较。您可能还希望 $currentTime 成为 Unix 时间戳整数。尝试使用

$currentTime = strtotime("now")

完整代码:

$currentDay = date("N");
$currentTime = strtotime("now");

if ( ($currentDay == 4) && ($currentTime >= strtotime("19:55:00")) && ($currentTime <= strtotime("21:05:00")) ) {
    functionX();
} else {
    functionY();
}
于 2013-07-18T00:42:24.423 回答
0
$schedToday = '11am-5pm';

isBusinessOpen($daytoday]);

function isBusinessOpen($time_str){
    //Get the position of the dash so that you could get the start and closing time
    $cut = strpos($time_str, '-');
    
    //use substring to get the first windows time and use strtotime to convert it to    
    $opening_time = strtotime(substr($time_str, 0, $cut));
    
    //same as the first but this time you need to get the closing time  
    $closing_time = strtotime(substr($time_str, $cut + 1));
    
    //now check ifthe closing time is morning so that you could adjust the date since most likely an AM close time is dated tomorrow
    if(strpos(strtolower(substr($time_str, $cut + 1)), 'am')){
        $closing_time = strtotime(date('m/d/y') . ' ' . substr($time_str, $cut + 1) . ' + 1 day');
        $opening_time = strtotime(date('m/d/y') . ' ' . substr($time_str, 0, $cut));
    }
    //to get the current time. take note that this will base on your server time
    $now = strtotime('now');    

    // now simply check if the current time is > than opening time and less than the closing time
    return $now >= $opening_time && $now < $closing_time;
}
于 2020-06-22T17:16:45.703 回答