Jump to content

[SOLVED] Date comparison


thecase

Recommended Posts

Hi,

 

I have the date like this Friday 26 June 2009 how can I create a if statment to check if that time is the present or in the past

 

Thanks

 

Checks if $day matches the current day.  Something like this:

 

$day = "Monday 29 June 2009";
$now = date("l j F Y");
echo ($day==$now) ? "today" : "not today";

?>

The above solution is good if you want to determine if the input date is the current date, but not to determine if it is a past date since it may also be a future date.  You can use strtotime to extract a timestamp from the input date and then compare the timestamps of the input date and the current date.  Here is a simple solution:

 

$new_date = strtotime('Friday 26 June 2009');
$current_date = strtotime(date('l j F Y'));
if($new_date < $current_date)
    echo 'input date is a previous date';
elseif($new_date > $current_date)
    echo 'input date is a future date';
elseif($new_date == $current_date)
    echo "input date is today's date";

how can I create a if statment to check if that time is the present or in the past

 

He only specified present or past.  So I think it's safe to speculate that he doesn't allow users to choose a date in the future, but if he does, then good point.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.