0

I'm sure this is a pretty basic task, but I'm quite new to programming and am a bit confused.

Basically I want to execute a different action based on the time of day. For example, if the time is between 05:00 and 20:00 (8pm), run "scriptA". ELSE, or if time is between 20:00 and 05:00, run "scriptB".

What is the easiest way to do this? I basically want to run one script during the "Day" and the other at "Night".

Thank you!

3 Answers3

1

You can try using the date function.

echo date("H:i:s");

will display the current time. Get this string. Compare it and use an if/else.

PHP Date Reference Manual

0
<?php
if((date("H") > 5) && (date("H")<20) )
{

//run_my_day_script

}

else
{
//run_my_evening_script

}
?>
Charles
  • 427
  • 3
  • 6
0

Get the hour of the day in a 24 hour format, without leading zeros.

$hour = (int)date("G");

Then you can do a less or greater that check.

if ($hour >= 5 && $hour <= 20)
{
  // Do Something.
}

http://php.net/manual/en/function.date.php

WhoIsRich
  • 4,053
  • 1
  • 33
  • 19