Connecting Tech Pros Worldwide Forums | Help | Site Map

changing a variable based on time of day

Craig Keightley
Guest
 
Posts: n/a
#1: Jul 17 '05
I want to set a value of a variabel $var based on the time of day
eg
if time is between 10:00pm and 9:00 am
set $var = "foo"

if time is between 9:00 am and 7.00pm
set $var = "bar"

if time is between 7.00pm and 10.00pm
set $var to "foobar"


how do i do this with the timestamp function?
many thanks

craig



Ken Robinson
Guest
 
Posts: n/a
#2: Jul 17 '05

re: changing a variable based on time of day



Craig Keightley wrote:[color=blue]
> I want to set a value of a variabel $var based on the time of day
> eg
> if time is between 10:00pm and 9:00 am
> set $var = "foo"
>
> if time is between 9:00 am and 7.00pm
> set $var = "bar"
>
> if time is between 7.00pm and 10.00pm
> set $var to "foobar"
>[/color]

Here's one way of doing it:
<?
$now_time = date('G',strtotime('now'));
echo $now_time."<br>\n";
$var = 'foo';
switch (TRUE) {
case ($now_time >= 9 && $now_time <= 19):
$var = 'bar';
break;
case ($now_time > 7 && $now_time <= 22):
$var = 'foobar';
break;
}
echo '$var:' . $var . "<br>\n";
?>

Ken

NC
Guest
 
Posts: n/a
#3: Jul 17 '05

re: changing a variable based on time of day


Craig Keightley wrote:[color=blue]
>
> I want to set a value of a variabel $var based
> on the time of day
> eg
> if time is between 10:00pm and 9:00 am
> set $var = "foo"
>
> if time is between 9:00 am and 7.00pm
> set $var = "bar"
>
> if time is between 7.00pm and 10.00pm
> set $var to "foobar"
>
> how do i do this with the timestamp function?[/color]

First of all, there is no timestamp() function in PHP.
You need to use date() function:

$hour = date('G');
$var = "foo";
if (($hour >= 9) and ($hour < 19)) {
$var = "bar";
}
if (($hour >= 19) and ($hour < 22)) {
$var = "foobar";
}

Cheers,
NC

Closed Thread