set php code to be run in the future

PHP – Scheduled code fragments or set code to be run in the future

Sometimes you may need a code which should be run in certain date interval. No, i don’t mean whole php file, for that there is a technology called “Cron Jobs”.
I mean code fragment in your existing PHP project.
For example you want you background to be red after 23:59. Or at 31st decebmer night you want some “happy new year” banner to be appear for 1-2 hours. Or you just creating some scheduled event module and this logic can help you build it.
Let’s write this simple trick.

set php code to be run in the future

$now_date=time();
if ($now_date>1475602004 and $now_date<1475638904) {
echo 'body {background: red}';
}

or we can use readable dates.

$now_date=time();
$begdate=strtotime('2016-10-05 23:59:00');
$enddate=strtotime('2016-10-06 01:59:00');
if ($now_date>$begdate and $now_date<$enddate) {
echo 'body {background: red}';
}

That’s all. This simple php code runs code fragment in the given datetime interval.