Josh Gandosh Josh Gandosh - 7 months ago 15
PHP Question

Is there more the right way to compare between present date and a deadline?

Is there more the right way to compare between present date and a deadline?

date_default_timezone_set('Asia/Jakarta');
$deadline = date('2016-04-26 23:59:59');
$deadline = strtotime($deadline);
$now = strtotime("now");

if ($now > $deadline) {
echo 'Deadline has passed.';
} elseif ($now === $deadline) {
echo 'The same time.';
} else {
echo 'You have time';
}

Answer

The code you've given should work as intended, but is somewhat "older" style. The "modern" version would be using the DateTime family of objects.

$tz = new DateTimezone('Asia/Jakarta');
$deadline = new DateTime('2016-04-26 23:59:59', $tz);
$now = new DateTime('now');

if ($now > $deadline) {
    echo 'Deadline has passed.';
} elseif ($now === $deadline) {
    echo 'The same time.';
} else {
    echo 'You have time';
}

One specific advantage of this approach is that you do not have to globally modify the default timezone.