Page 1 of 1

if between times

Posted: Thu Jul 19, 2007 9:27 am
by aceconcepts
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?

Posted: Thu Jul 19, 2007 10:07 am
by Begby
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.

Posted: Thu Jul 19, 2007 10:11 am
by pickle
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';

Posted: Thu Jul 19, 2007 10:33 am
by aceconcepts
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.