douju1953 2015-04-12 11:55
浏览 80
已采纳

如何确定当前日期/时间是否在计划内?

Given the schedule:

{
  "start_day": "Monday",
  "end_day": "Friday",
  "start_time": "9:00 AM",
  "end_time": "6:00 PM"
}

How can I determine if the current time in a specified time zone falls within the above given schedule? Are there any functions in PHP that'll help me? Issues that I foresee:

  1. Dealing with time zones
  2. The given schedule doesn't provide year information, but I guess I can assume it applies to the current year
  3. Dealing with the range of days (Monday and Friday are provided; how do I know that there are Tuesday, Wednesday, and Thursday in between?

UPDATE 1:

Change JSON so it encompasses all days/times:

{
  "start_day": "Monday",
  "end_day": "Sunday",
  "start_time": "12:00 AM",
  "end_time": "11:59 PM"
}

Then change $now assignment to $now = new DateTime("Saturday next month 10 am", $timezone);

Nothing is outputted.

  • 写回答

1条回答 默认 最新

  • doumei7420 2015-04-12 13:22
    关注

    You can use the following code. I check for all available timezones in that example:

    <?php
    
    $json = <<<EOF
    {
      "start_day": "Friday",
      "end_day": "Sunday",
      "start_time": "9:00 AM",
      "end_time": "6:00 PM"
    }
    EOF;
    
    $when = json_decode($json);
    
    // Get weekdays between start_day and end_day.
    $start_day = new DateTime($start);
    $nstart_day = (int) $start_day->format('N');
    $end_day = new DateTime($end);
    $nend_day = (int) $end_day->format('N');
    
    // If the numeric end day has a lower value than the start
    // day, we add "1 week" to the end day
    if($nend_day < $nstart_day) {
        $end_day->modify('+1 week');
    }
    // Add one day to end_day to include it into the return array
    $end_day->modify('+1 day');
    
    // Create a DatePeriod to iterate from start to end
    $interval = new DateInterval('P1D');
    $period = new DatePeriod($start_day, $interval, $end_day);
    $days_in_between = array();
    foreach($period as $day) {
        $days_in_between []= $day->format('l');
    }
    
    
    // Check for all timezones in this example
    foreach(DateTimeZone::listIdentifiers() as $tzid) {
        $timezone = new DateTimeZone($tzid);
        // Current time in that timezone
        $now = new DateTime("now", $timezone);
        // start_time in that timezone
        $start_time = new DateTime($when->start_time, $timezone);
        // end_time in that timezone
        $end_time = new DateTime($when->end_time, $timezone);
        // Get weekday for that timezone
        $day = $now->format('l');
    
        if($now >= $start_time && $now <= $end_time
            && in_array($day, $days_in_between))
        {
            printf("In %s the current time %s is between %s and %s (%s-%s)
    ",
                $tzid, $now->format('H:i:s'), $when->start_time, $when->end_time,
                    $when->start_day, $when->end_day);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?