Connecting Tech Pros Worldwide Help | Site Map

changing a variable based on time of day

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 11:27 AM
Craig Keightley
Guest
 
Posts: n/a
Default changing a variable based on time of day

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, 11:27 AM
Ken Robinson
Guest
 
Posts: n/a
Default 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, 11:27 AM
NC
Guest
 
Posts: n/a
Default 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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.