Connecting Tech Pros Worldwide Help | Site Map

changing a variable based on time of day

  #1  
Old July 17th, 2005, 12:27 PM
Craig Keightley
Guest
 
Posts: n/a
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


  #2  
Old July 17th, 2005, 12:27 PM
Ken Robinson
Guest
 
Posts: n/a

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

  #3  
Old July 17th, 2005, 12:27 PM
NC
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
merits of Lisp vs Python Mark Tarver answers 852 March 15th, 2007 09:25 PM
WAP, VS2005 keeps changing my user control types!! Tim_Mac answers 3 September 11th, 2006 02:15 AM
Roundup of FAQ change requests Richard Cornford answers 4 July 23rd, 2005 04:44 PM
Variable inside a nested loop r rk answers 3 July 23rd, 2005 08:09 AM