Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Date larger than current date

Tags:

date

php

I have this code:

$curdate = '22-02-2011';  $mydate = '10-10-2011';                       if($curdate > $mydate) {     echo '<span class="status expired">Expired</span>'; } 

This would echo expired BUT shouldn't because $mydate is in the future and therefore smaller than the $curdate but PHP is looking at JUST the first two numbers 22 and 10 instead of the whole string. How can I fix this?

Thanks

like image 826
Cameron Avatar asked Feb 22 '11 18:02

Cameron


1 Answers

Try converting them both to timestamps first, and then compare two converted value:

$curdate=strtotime('22-02-2011'); $mydate=strtotime('10-10-2011');  if($curdate > $mydate) {     echo '<span class="status expired">Expired</span>'; } 

This converts them to the number of seconds since January 1, 1970, so your comparison should work.

like image 118
Zak Avatar answered Oct 05 '22 11:10

Zak