Re: compare dates

From: Date: Thu, 01 Dec 2011 20:55:48 +0000
Subject: Re: compare dates
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-315916@lists.php.net to get a copy of this message
On 01-12-2011 02:17, Floyd Resler wrote:
On Nov 30, 2011, at 5:04 PM, Matijn Woudt wrote:
On Wed, Nov 30, 2011 at 11:00 PM, Marc Fromm<Marc.Fromm@wwu.edu> wrote:
I'm puzzled why the if statement executes as true when the first date (job_closedate) is not less than the second date (now). The if statement claims that "12/02/2011" is less than "11/30/2011".
                if (date("m/d/Y",strtotime($jobs_closedate))<= date("m/d/Y",strtotime("now"))){
You're comparing strings here, try to compare the unix timestamp: if (strtotime($jobs_closedate)<= strtotime("now")){ That'll probably do what you want.. Matijn
Another way to do it would be: if(strtotime($jobs_closedate)<=time()) { } or if(date("Y-m-d",strtotime($job_closedate))<=date("Y-m-d",time()) { } Take care, Floyd
As of PHP 5.2.2 direct comparison of DateTime objects is also possible. So: $closeDate = new DateTime($jobs_closedate); $now = new DateTime('now'); if($closeDate < $now) { echo $closeDate->format('m/d/Y'); // output in US date format echo $now->format('m/d/Y'); // output in US date format $error .= "The close date must be later than today's date, " . $now->format('m/d/Y') . "\n"; } This, IMO is a lot prettier. - Tul

« previous php.general (#315916) next »