Home   Best Sellers   Blogging   Coding & Design   Technology   SEO   Travel & living   Career   Videos   Tips   Online tools     
Home  »     »     »     »  Compare two dates using php string comparison

Compare two dates using php string comparison

Thursday, June 27, 2013

Here is a small script in PHP to compare 2 dates
This comparison will work if dates are in "YYYY-mm-dd" format

Try this Your self

$date1="2012-05-20"; $date2="2012-05-20";

if( $date1 > $date2 ) echo "$date1 greater than $date2";
elseif( $date1 == $date2 ) echo "$date1 equal to $date2";
else echo "$date1 lower than $date2";

Just changed this to a function

function strDateCompare($date1,$date2){
if( $date1 > $date2 )
return "$date1 greater than $date2";
elseif( $date1 == $date2 )
return "$date1 equal to $date2";
return "$d1 lower than $d2";

call the function as given below:

echo( strDateCompare("2012-05-20","2012-05-20") );
//this will return "2012-05-20 equal to 2012-05-20"
echo( strDateCompare("2011-03-15","2012-05-20") );
//this will return "2011-03-15 less than 2012-05-20"

echo( strDateCompare("2012-05-20","2011-03-15") );
//this will return "2012-05-20 greater than 2011-03-15"

Hope this helps someone
thanks :)

Share this!

How to link to this page?
If you wish to link to this page from your website, simply Copy and paste the above HTML code to your web page. It will appear on your page as:
Compare two dates using php string comparison.

comments powered by Disqus

This Weeks 7 Popular Posts

Subscribe to Recent Posts by Email
Stay connected to CROZOOM with regular Email notices of new Techie articles and IT Jobs. Updates will be delivered to your Inbox as soon as they are posted online.

Enter Your Email Address:  

Delivered by FeedBurner   RSS Feed

Search this Blog   

Urgent Openings for PHP trainees, Andriod / IOS developers and PHP developers in Kochi Trivandrum Calicut and Bangalore. Please Send Your updated resumes to recruit.vo@gmail.com   Read more »