String to Time with PHP <= 5.1.6 (DateTime::CreateFromFormat not available)?
All I need is to compare dates represented by string values in the format d.m.y (e.g. 15.07.11
for today).
strtotime
unfortunatelly gives me wrong results (probably because of the format). I found a answer to my question (PHP strtotime incorrect conversion) but DateTime::CreateFromFormat
is not available in my PHP ver开发者_如何转开发sion. I could not find strfptime
but think the autor of the answer meant strptime
. The result of strptime
is a array. I was surprised that I can compare arrays but the compare result is not valid.
What would be the easiest way to compare dates in the given string format with PHP <= 5.1.6?
You could always pass the result of strptime
to mktime
and get a usable Unix timestamp which you can compare or feed to the DateTime
objects.
<?php
$d = strptime('15.07.11', '%d.%m.%y');
$timestamp = mktime($d['tm_hour'], $d['tm_min'], $d['tm_sec'], $d['tm_mon'], $d['tm_mday'], 1900 + $d['tm_year']);
echo date("j F Y", $timestamp);
?>
The only thing to watch for is that strptime gives the year as the number of years since 1900, and mk_time just takes a year number, so I added 1900 to it.
精彩评论