Comparing two dates

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
terji
Forum Commoner
Posts: 37
Joined: Tue May 14, 2002 5:27 pm
Location: Denmark

Comparing two dates

Post by terji »

I need to compare two dates to see which one is newer. What am I doing wrong in this example:

Code: Select all

$date1 = date("d-m-y H.i", mktime(0, 0, 0, 2, 3, 2003));
$date2 = date("d-m-y H.i", mktime(0, 0, 0, 2, 4, 2003));

if($date1 < $date2)
{
     print("date2 is newer than date1");
}
I'm I totally going in the wrong direction here or what?
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

lose the date() bit so you are just comparing timestamps and not strings

$date1 = mktime(0,0,0,2,3,2003);
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Exactly - compare the timestamps and save the formatting for when you display the data.

Mac
terji
Forum Commoner
Posts: 37
Joined: Tue May 14, 2002 5:27 pm
Location: Denmark

Thx

Post by terji »

Thank you! Works like a charm....
Post Reply