lundi 21 mars 2016

Does PHP support proper time comparisons if-statement?

I want to check whether it is working times using PHP. But I wasn't able to do a proper time comparison using any of the date functions. Does PHP support time comparisons with the use of date functions or not?

Currently I solved my issue by calculating the amount of the minutes of the day have passed:

<?php
    date_default_timezone_set('Europe/Amsterdam');
    $hours = date('G');
    $minutes = date('i');
    $totalMinutes = ($hours * 60) + $minutes;
    $betweenMinutes1 = (8 * 60) + 30;
    $betweenMinutes2 = (17 * 60) + 0;

    echo $hours.' * 60 + '.$minutes.' = '.$totalMinutes.' minuten van de 1440 op een dag verstreken <br/><br/>';

    echo 'if '.$totalMinutes.' is between '.$betweenMinutes1.' and '.$betweenMinutes2.' it is workingtime:';

    if (!($totalMinutes > $betweenMinutes1 && $totalMinutes < $betweenMinutes2)) {
        echo 'It isn\'t workingtime.';
    } else {
        echo 'It is workingtime.';
    }
?>

Is this this the best way, or is there a way to do this using the proper date functions within the if-statement

Aucun commentaire:

Enregistrer un commentaire