All across my php code i'm storing dates and times in UTC, but i'm also using mysql to store datetimes (also in utc).
is there any way that date compariso开发者_如何学JAVAns can fail with the greater than and less than operator?
$curdate=date('Y-m-d H:i:s');
if($start_datetime>$curdate)
Nope.
There is no way for them to fail.
Mysql date format is intentionally made for this purpose.
There is not a single reason to convert it in whatever else format to compare.
PHP:
change date into UNIX timestamp using strtotime()
and then u can compare.
MySQL:
change dataType of date column to DateTime
and then u can compare below way:
$d1 = new DateTime('2008-08-03 14:52:10');
$d2 = new DateTime('2008-01-03 11:11:10');
var_dump($d1 == $d2);
var_dump($d1 > $d2);
var_dump($d1 < $d2);
A direct datetime compare won't fail. You can do that.
A timestamp comparison would be faster, but I don't think such a micro-improvement in performance would be something to look for in a php application, plus you'll have to take into account the 2030 bug.
I prefer to compare the 'Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)'. Of course, this is only good for dates on/after 1970.
Given that $date1 and $date2 are two date variables to compare:
if (date("U",$date1) > date("U",$date2)) {
// $date1 is more recent than $date2
} else {
// $date1 is older than (or equal to) $date2
}
strtotime($curdate) < strtotime($start_datetime)
strtotime() returns a int rperesenting the Seconds that passed since 01.01.1970 That means if $curdate date is "2011-01-01 12:00:00" and $start-datetime is "2011-01-01 12:00:01" then $start-datetime is bigger then $curdate because strtotime returns an integer for $start-datetime that is exactly one bigger than $curdate.
精彩评论