PHP How to compare date and date?

I have this value from database:

'2009-1-1 00:00:00', okay, let me paste my code:

$fetch = mysql_fetch_assoc($result);
$db_value = $fetch['date'];//'2009-1-1 00:00:00'

$today = date('Y-m-d H:i:s'); // Todays date

If I want to compare the two values, what should I do:

if($db_value < $today){
    // Do something
}

or method 2, convert to strtotime:

if(strtotime($db_value) < strtotime($today)){
    // Do someting
}

Maybe my method is not correct, what should I use to compare 2 dates?

Answers


Probably , you can get the epoch value of the date using mktime function , then you could

compare the date's times stamp values easily . before that you parse first date string

into year, month , day , hours, minutes , seconds. then use mktime function


You should use the second one, because your db result is a string so it can't be used like that.


You can use simple PHP date class to compare dates. It makes comparison very easy.

For example:

$date = new simpleDate();
echo $date->set($date1)->compare($date2)->isBefore();

There are also useful other comparison methods in the library. Please check the tutorials page. Click here.


Need Your Help

IIS error page is preventing me finding an error in ASP.NET Web API

asp.net .net exception-handling asp.net-web-api

I'm using ASP.NET web API. At some point an exception is being thrown before the action is being executed (possibly during DI for the controller), but I can't track it down because I just get a can...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.