Page 1 of 1
Assign value to a variable depending on time
Posted: Wed Feb 12, 2003 11:41 am
by real_AOD
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.

Posted: Wed Feb 12, 2003 11:49 am
by daven
Try using datetime functions (date(), time() mktime()) and conditional statements (if($my_time<$limit) $var=value)
Posted: Wed Feb 12, 2003 11:51 am
by hurkle
I'm a little rushed, so I don't have time to give you the complete code you need. I will point you in the right direction..
You can use php's date and time functions to get the current time, and pull out just the hours segment of that time value. You can then use a 'switch' control structure to do whatever you like depending on the time.
It might be even easier to build an array, where the key is an integer relating to the hour of the day, end the value is whatever you want to assign to your second variable.
Hope this helps get you started..
Posted: Wed Feb 12, 2003 12:13 pm
by real_AOD
I'm a complete newbie and I just want to use a simple code snippet for a pre-made script so could you be more specific on the coding? Thanks.

Posted: Wed Feb 12, 2003 1:00 pm
by BigE
There really isn't any code snippets like that out there that I'm aware of... you might want to just try coding your own. You could easily do it using switch() and some of the time functions listed above.
Posted: Wed Feb 12, 2003 1:06 pm
by volka
http://www.php.net/manual/en/function.getdate.php
http://www.php.net/manual/en/control-st ... switch.php
e.g.
Code: Select all
<?php
$date = getdate();
switch($date['hours'])
{
case 2: // 2am
case 3: // 3am
$value = 50;
break;
case 4:
...
}
?>
or
Code: Select all
<?php
$date = getdate();
if ($date['hours'] > 1 AND $date['hours'] < 16)
$value = 50;
elseif($date['hours'] > 15 AND $date['hours'] < 22)
$value = 20;
elseif
...
else
...
?>
Posted: Wed Feb 12, 2003 1:43 pm
by real_AOD
Got these somewhere and any would suffice:
/////////////////////////
<?
$num = date('H');
if($num >= 1 && $num <= 6) {
$variable = between 1am & 6am;
} elseif($num >= 7 && $num <= 12) {
$variable = between 7am and midday;
} elseif($num >= 13 && $num <= 18) {
$variable = between 1pm and 6pm;
} else {
$variable = dodgy number, or between 7pm and midnight;
}
?>
///////////////////////
or
//////////////
<?
$switchvar = 0;
$hour = date('H');
if ($hour <= 16 and $hour >= 2) $switchvar = 50;
echo $switchvar;
?>
///////////////
or
/////////////
<?
$x = date("G");
if($x >= 2 && $x < 16) $tvar = 50;
else $tvar = 0;
?>
//////////////
or
///////////////
<?
// get the current time
$current_time=time();
echo $current_time;
// format time to current hour
// using "G" format for current hour 0-23
$current_hour=date("G",$current_time);
echo "<br>" . $current_hour;
// set variable based on hour
if($current_hour == 14 || $current_hour == 15) {
$var = 50;
echo "<br>" . $var;
}
?>
///////////////////////
Anyways, thanks everyone for the replies!

Posted: Wed Feb 12, 2003 1:46 pm
by real_AOD
I have a problem on the above codes though. What if I want to breakdown the variable assignment in 30-minute intervals for example?
How do I use the date function to return the value in minutes or seconds?
Posted: Wed Feb 12, 2003 1:51 pm
by hurkle
This is an awesome oppurtunity for you to dive in and get your feet wet with some of the most useful php functions around.
Click on the following link:
http://www.zend.com/manual/
it's the php manual online. Scroll down to Function Reference -> Date and Time functions, and click on that link. It will show you examples of how the functions you need work, and how to get the minutes info you need. Lot's of handy examples. I highly reccomend digging through this manual, you'll be amazed.
Posted: Wed Feb 12, 2003 4:47 pm
by real_AOD
Thanks but I already have the latest searchable PHP manual in CHM format. I'm going to study it pretty soon.
What I need are some tips or tricks from experienced coders right now.
Here's my situation now:
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?

Posted: Wed Feb 12, 2003 4:56 pm
by hurkle
To be honest, it would have taken you less time to:
click on the link to the manual,
click on the 'date functions' link,
and scroll down one page to a detailed description of
exactly what you could use instead of just 'G',
than it did for you to:
post again, asking for clarification and help on this function.
There comes a point when you have to break down, and spend the 30 seconds it will take you to read the description of how to use the date function. If you're not willing to do that, it kind of implies that you place a much higher value on your own time than you do on the time of the people from whom you're requesting help.
Go ahead and look at the function reference, it won't hurt. By doing so, you'll impress the folks here, and probably at the other forums you've posted the same question( I say that 'cause I've seen your posts).
Best of luck.
Posted: Wed Feb 12, 2003 5:55 pm
by real_AOD
Thanks for your honesty and some bit of sarcasm hurkle. I'm really going to study programming soon and PHP will be my choice. Once I'm learned, I'll do my share of helping other newbies like me right now.
I will never ever go out of my way just to impress people. I'm just in a pressure situation right now so I can't concentrate much on analyzing.
I'm in a totally different profession and I just fell in love recently to web development and trying all avenues to speed up learning and at the same time my productivity (I'm running live websites now).
I've read the date functions but I still lack the coder way of thinking to put them into action. Like what I said I'm a newbie and if newbies are not welcome here then I think I should go.
"Ask a question and you're a fool for three minutes; do not ask a question and you're a fool for the rest of your life." - Chinese Proverb
Posted: Wed Feb 12, 2003 6:05 pm
by hurkle
Newbies *are* totally welcome here, don't get the wrong impression. Also, there wasn't any sarcasm in my post; I really do think it's in your best interest to read the description of how the date function works. You're 99.9 percent of the way there.. you already now that using the 'G' parameter won't fit your needs.. the function description will tell you what might.
When you're this close to figuring out the right answer, I wouldn't be doing you any favors by handing you something you can get for yourself with literally a couple minutes work. I'm not being mean, this is just something I feel strongly about.
"Give a man a fish and you feed him for a day. Teach
a man to fish and you feed him for a lifetime." Chinese Proverb.
( I added this 'cause it's directly related to my point)
Posted: Wed Feb 12, 2003 6:25 pm
by real_AOD
Hurkle!

Peace! or Fish?
Yeah I'll learn fishing pretty soon...

Posted: Thu Feb 13, 2003 2:47 pm
by real_AOD
There you go:
if (strftime("%H:%M",time())>"14:00" && strftime("%H:%M",time())<"16:00")
$variable=50;
elseif ..........
