-1

How to compare two dates in php. I got an array of dates from the database and I want it to compare with todays date.

I have something like this.

$d['dd'] = 10;
$d['mm'] = 12;
$d['yy'] = 13;

Now I have done Something like this:

$date = $d['yy'].'-'.$d['mm'].'-'.$d['dd'];
$date_diff=strtotime(date("y-m-d"))-strtotime($date); 
if($date_diff>0){
     //this date is gone.
}
 // I want to show this day will go in 5 days. To do this I have following.      
if((strtotime(date("y-m-d", strtotime(date("y-m-d", strtotime(date('y-m-d'))) . "+ 5 days")))-$date_diff)>0){
// This day will go in 5 days time.
}

Now my question is what is the best way to do this or how can I do it in a better(more reliable) way. Any suggestions will be helpful.

Thanks in advance.

modon
  • 241
  • 3
  • 7
  • 14

1 Answers1

0

When doing date comparision you should do it directly in the database (if possible), as mysql knows about timezones, DST-stuff, leap years etc. PHP DateTime has a history of problems, and if you can get the database to do this I would recommend this.

Save your dates as DateTime and use the built in mysql date-functions: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

user2849406
  • 195
  • 1
  • 1
  • 6