如何使用给定的时区/开始/结束验证当前日期/时间?
在$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();
答案 0 :(得分:2)
解决方案很简单,但你犯了一些错误。顶部的变量不在全局范围内。它们在函数内部不可用。因此,您需要将它们放在函数内部,或者将它们作为参数传递(就像我在下面的代码中所做的那样)。之后,使用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)