Forum Moderators: coopster

Message Too Old, No Replies

Changing destination based on Time

         

internetheaven

10:15 am on Nov 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I want to change the content of a page based on the time of day. I've got javascript that can do it but I'm having trouble writing the php as the number of date and time functions is really confusing and I can't seem to find a "greater than AND less than" function so am resorting to just listing the hours one by one.

Any help would be appreciated:

<?php
if ($hour = 23) {
echo "It's almost midnight!";
}
if ($hour = 24) {
echo "It is midnight!";
}
if ($hour = 1) {
echo "It's one in the morning.";
}
if ($hour = 2) {
echo "2am? Getting late ...";
}
if ($hour = 3) {
echo "3am? Are you early?";
}
else {
echo "Good day to you";
}
?>

I hope you can see what I'm trying to accomplish (obviously those aren't the content variations I will actually be doing!)I would have thought there would be something to allow me to do it in one line e.g. "if ($hour > 23 and < 7)" but I can't find anything.

Thanks
Mike

henry0

12:23 pm on Nov 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How will do a switch, using a TIME format
and calling for (your content) based on an id #
like when error is called ie: error #2 ... etc...

try a few solutions and bench-test
I am not sure that it helps
but I am curious about what other member will come with :)

Mr_Fern

12:32 pm on Nov 30, 2007 (gmt 0)

10+ Year Member



if you want a between you just do (x > a && x < b)

if ($hour >= 6 && $hour < 12) // 6am to 11:59am
{ executeMorningLayout(); }
elseif ($hour >= 12 && $hour < 18) // 12pm to 5:59pm
{ executeAfternoonLayout(); }
elseif ($hour >= 18 && $hour < 23) // 6pm to 10:59pm
{ executeEveningLayout(); }
else // effectively: if ($hour < 6 ¦¦ $hour >= 23)
// 11pm to 5:59am
{ executeSleepTimeLayout(); }

internetheaven

1:10 pm on Nov 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's great, thanks Mr_Fern - I've been crawling for hours trying to find out about betweens, I thought it would have been easier!

Which "time" coding is the best to use for this sort of thing? I'm having trouble understanding them all and to which is easier to parse to find out what hour it is on the server when the script is called.

mktime
strftime
strptime
strtotime

all seem very complicated ways of doing it.

P.S. Is php.net still the best place to research this sort of thing?

Mr_Fern

1:29 pm on Nov 30, 2007 (gmt 0)

10+ Year Member



Since you're basing it off the server's time

$hour = (int) date('H'); // get current hour

should be fine.

function date(string dateFormat, [int timestamp])
dateFormat = format for the date (formatting options can be found php.net site)
timestamp = number for the date/time you want represented, defaults to current time

the format option H gives you the hour of a timestamp in 24 hour format in a string format (00 to 23). Putting the (int) in front of it forces it to be an integer so you'll have 0 to 23 as a result.

Yeah php.net would be the best resource I think.