View Full Version : Assign value to a variable depending on time

02-12-2003, 06:52 PM
How do I assign a value to a variable depending on the time of the day?

For example: If time is between 2am to 4pm, variable = 50.

How can I accomplish this?

Thanks for any help. :)

02-12-2003, 07:50 PM
As long as you remember that you are dealing with a 24 hour clock, and have a firm grasp of greater/lesser than and greater/lesser or equal to, it's no problem.

$switchvar = 0;
$hour = date('H');
if ($hour <= 16 and $hour >= 2) $switchvar = 50;
echo $switchvar;

02-12-2003, 08:48 PM
Thanks Feyd. That would get me started. :)

What if I want to breakdown the variable assignment in 30-minute intervals for example? :confused:

How do I use the date function to return the value in minutes or seconds?

02-12-2003, 10:04 PM
Minutes is i (00 - 59), seconds is s (00-59)


$minute = date('i');


$second = date('s');


02-12-2003, 11:36 PM
But that would only give me values from 0 to 60 for a given hour or for a given minute.

I want the date function to return values in seconds or minutes for a day instead of just 0 to 23 (using the "G" format).

Like so:

0 to 1440 (one whole day by the minutes)

0 to 86400 (one whole day by the seconds)

For example between 7:16 am to 7:46 am, variable = 12. The "G" format is inadequate for this.

Any ideas? :)

02-14-2003, 01:38 AM
maybe u should try to do like this

$hour = date('H);
$minute = date('i');
$second = date('s');'

# now u can calculate how many seconds elapsed so far today!

$number = ($hour*60*60)+($minute*60)+$second;

# then u do assign any value to your variable acording to $number.

hope it helped.


02-14-2003, 03:19 AM
Thanks djbruno it helped. :)

Or I can also try something like this:

if (strftime("%H:%M",time())>"14:00" && strftime("%H:%M",time())<"16:00")
elseif ......