PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
aceconcepts
DevNet Resident
Posts: 1424 Joined: Mon Feb 06, 2006 11:26 am
Location: London
Post
by aceconcepts » Thu Jul 19, 2007 9:27 am
Hi,
I am trying to figure out how one could determine what time of the day it would be (morning, afternoon or evening) when someone enters a time in a text field.
I know this is the incorrect way of doing it but it gives a clear example:
Code: Select all
if($sTime>=00:00:00 && $sTime<12:00:00)
{
$sTOD = "Morning";
}
elseif($sTime=>12:00:00 && $sTime<18:00:00)
{
$sTOD = "Afternoon";
}
elseif($sTime=>18:00:00 && $sTime<00:00:00)
{
$sTOD = "Evening";
}
Any thoughts?
Begby
Forum Regular
Posts: 575 Joined: Wed Dec 13, 2006 10:28 am
Post
by Begby » Thu Jul 19, 2007 10:07 am
Give this a try and let me know if it works.
Code: Select all
$sTime = strtotime($sTime) ;
if ($sTime >= strototime('12:00am') && $sTime < strtotime('12:00pm') )
{
echo 'morning' ;
}
elseif (stuff)
{
//etc...
}
strtotime is a wonderfully fun function, you can even do things like strtotime('Next Monday') and it will return the correct unix timestamp.
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Thu Jul 19, 2007 10:11 am
Pretty much just how you've done it.
Code: Select all
if(hour < 12)
sTOD = morning;
else if( hour < 18)
sTOD = Afternoon;
else
sTOD = Evening
or if you wanted to get really obfuscated:
Code: Select all
$sTOD = ($hour > 12) ? ($hour > 18) ? 'Evening' : 'Afternoon' : 'Morning';
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
aceconcepts
DevNet Resident
Posts: 1424 Joined: Mon Feb 06, 2006 11:26 am
Location: London
Post
by aceconcepts » Thu Jul 19, 2007 10:33 am
Superb,
Thanks guys. I went with pickle's choice (so frikin simple - i love it!!!).
However, strtotime() is great and I'm sure I will be using it again and again.
Thanks again.