finalist finalist - 3 months ago 18
PHP Question

PHP how to check current DateTime and future DateTime until difference becomes 0

Is it possible to check current DateTime and future DateTime in every 30 seconds until difference becomes 0 ( current DateTime to be equal to the future DateTime ) with precision of a minute using PHP ?
My code is not continuously in every 30 seconds:

<?php
$dt_format = 'Y-m-d H:i';

$new_row = "<br>";

$dt_now = new DateTime();
$date1 = DateTime::createFromFormat($dt_format, '2016-09-01 14:00');
$date2 = DateTime::createFromFormat($dt_format, '2016-09-01 14:15');

print("Today is " . $dt_now->format($dt_format) . $new_row);

$diff1 = $dt_now->diff($date1);
print ($diff1->format('%y years %m months %d days %h hours %i minutes') . $new_row);

$diff2 = $dt_now->diff($date2);
print ($diff2->format('%y years %m months %d days %h hours %i minutes') . $new_row);
?>

Answer

Do you want to block the page load into the time matches? That will lead to resource exhaustion on your server if the target time is too long.

PHP is single threaded so you can't do this check in the background.

Better to write out a holding page for the user, which tries to reload every 30 seconds.

If the time is reached, show the content, otherwise show a page with this content:

<p>Please wait until (put target time)</p>
<script>
     setTimeout(function() {
          window.location.reload();
     }, 30000);
</script>