Forum Moderators: coopster
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
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(); }
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?
$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.