YumYumYum YumYumYum - 1 month ago 6
PHP Question

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

How can i make current date/time validate with given timezone/start/end?

outside

$meeting_for, $meeting_starts till $meeting_ends range
, all should return 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();

Answer

The solution is quite simple, but you made a few mistakes. The variables at the top aren't in global scope. They're not available inside the function. Therefor you either need to put them inside the function, or pass them along as parameters (as I did in the code below). After that it's a very simple check with an if statement:

<?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);

?>

Keep in mind that the echo() won't actually give any output. You need to return a string for that instead of a boolean.

EDIT:

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