如何验证当前时间是否与给定的时区开始和结束相匹配


PHP - how to validate if current time is matched with given timezone start and end

如何使当前日期/时间验证给定的时区/开始/结束?

$meeting_for, $meeting_starts till $meeting_ends range之外,都应该返回false。

$meeting_for = 'America/Los_Angeles';
$meeting_starts  ='2016-10-11 00:00:00';
$meeting_ends = '2016-10-11 06:00:00';
function give_meeting_result_based_on_rightnow() {
  // PHP server time
  date_default_timezone_set('Europe/Brussels');
  $etime1 = date('Y-m-d H:i:s'); 
  $date = new DateTime($etime1, new DateTimeZone('Europe/Brussels'));
  // PHP server time converted to meeting time
  $date->setTimezone(new DateTimeZone($meeting_for));  
  $logic_check=  $date->format('Y-m-d H:i:s') . "'n"; 
  if($logic_check is between ($meeting_starts till $meeting_ends )) {
    return true;
  } 
  else {
    return false;
  }
}
echo give_meeting_result_based_on_rightnow();

解决方案很简单,但是您犯了一些错误。顶部的变量不在全局作用域中。它们在函数内部不可用。因此,您要么需要将它们放入函数中,要么将它们作为参数传递(正如我在下面的代码中所做的那样)。之后是一个非常简单的if语句检查:

<?php
// These are NOT global. They're not available within the scope of the function
$meeting_for = 'America/Los_Angeles';
$meeting_starts  ='2016-10-11 00:00:00';
$meeting_ends = '2016-10-11 06:00:00';
function give_meeting_result_based_on_rightnow($timeZone, $startTime, $endTime) {
    // PHP server time
    date_default_timezone_set('Europe/Brussels');
    $etime1 = date('Y-m-d H:i:s'); 
    $date = new DateTime($etime1, new DateTimeZone('Europe/Brussels'));
    // PHP server time converted to meeting time
    $date->setTimezone(new DateTimeZone($timeZone));  
    $logic_check=  $date->format('Y-m-d H:i:s') . "'n";
    if ($logic_check >= $startTime && $logic_check <= $endTime)
    {
        return true;
    } else {
        return false;
    }
}
// Passing along the variables as parameters to the function
echo give_meeting_result_based_on_rightnow($meeting_for, $meeting_starts, $meeting_ends);
?>

请记住,echo()实际上不会给出任何输出。你需要返回一个字符串而不是一个布尔值。

编辑:

$ var_dump_this_code_with_curdate('2016-10-10 07:54:32')
bool(false)