I've run into something unusual. For some reason when adding a dateinterval with only minutes set in it makes it add 67 years.
$wTime = new DateTime("2011-05-17 01:54:56 +0000");
echo $wTime->fo开发者_如何学Crmat("d/m/Y H:i:s\n");
$wTime->add(new DateInterval("P810M"));
echo $wTime->format("d/m/Y H:i:s");
The result is:
17/05/2011 01:54:56
17/11/2078 01:54:56
I can't see where I'm doing anything wrong. Is this a bug in the DateTime object, or is something off with my code? I've run into annoying bugs with it in the past. I am running the latest version of PHP (5.3.6) built from source on Mac OS X 10.6
M
is for months, so this is adding 810 months (67.5 years). Use i
or I
for minutes.
$wTime->add(new DateInterval("P810I"));
The PHP manual page for DateInterval has a complete list of the recognized formats.
Check the docs, you're adding 810 months (~67 years). Try PT810M
You are actually adding 810 months instead of minutes. Try -
$wTime->add(new DateInterval("P810I"));
Also looks like the PHP documentation is wrong. But if you look at the example output on that page, you will realise that the code for minutes is 'i', not 'm'.
You added months, not minutes. Try i
instead of m
精彩评论