compare 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
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

compare two dates?

Post by lazersam »

Hi
Can anyone suggest how I might get two dates and determine which one is the latest?

for example;

$date1 = "27 of December 2003, at 1.44 pm"
$date2 = "01 of January 2004, at 1.44 pm"

How can I see if $date2 is later than $date1?

Do you think this is a good way.. and could I write it better?

Code: Select all

<?php
	
                #Todays Date
	$date1 = gmdate("U",time ());
	
	#Make a date 30 days ahead
	$date2 = gmdate("U",mktime(date(H) + 24 * 30 ));
	
	#Do the test.
	If (   $date1 > $date2  ){
	echo "date1 > date2";
	exit ();
	}

?>
Regards

Lawrence.
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

convert the dates into timestamp
the latest date will have high timestamp than the earlier one .
Post Reply