Log in

View Full Version : Working with dates



JasonDFR
02-05-2009, 07:09 AM
How would I test for a range of dates, regardless of the year?

For example:

If ( between April 1st and November 1st )
DO THIS
else
DO THIS

I just realized, maybe:


$month = date(m);
if ( $month >= 4 && $month <= 10 )
// Do this
else
// Do this

Maybe writing it out helped.

Is there a better way of doing it?


Thanks

bluewalrus
02-05-2009, 02:09 PM
Did that work like that? I thought that the date was a string, which wouldn't be comparable with an integer.

JasonDFR
02-05-2009, 03:12 PM
$month = date(m);
if ( $month >= 4 && $month <= 10 )
echo "It is between April and November.";
else
echo "It isn't between April and November.";


Yeah, the above works perfectly. date() does return a string, but depending on what context the string is used in, PHP knows how to handle it. In this case comparing it to numerical values. This is why you'll hear or read that PHP is a "loosely typed" language. The only time you'll have problems is if you try to use a string in a function that explicitly requires an integer.

sysout
02-06-2009, 03:33 AM
maybe this will work :


$date1 = date("d m Y");
//$date2 fetch date from database (ex : creation date)

//explode date1 and date2 :
$temp1 = explode(" ", $date1);
$month1 = $temp1[1]; //the 2nd array

$temp2 = explode(" ", $date2);
$month2 = $temp2[1]; //the 2nd array

//compare them :
if($month1 >= $month2){
echo "You Create Programs After Today";
}
else{
echo "You Create Programs Before Today";
}


I haven't tried this code, CMIIW, hope that I can help you