1

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->format("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

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Check the docs, you're adding 810 months (~67 years). Try PT810M

2 Comments

Thanks, this fixed it. I can't believe I made a post about such a stupid mistake. How shameful.
No worries. I'll admit the fact that PHP has M for both month and minutes is pretty lame. Looks like I is an alternative for minutes format. But from the docs, T is the documented way to specify M as minutes.
0

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'.

1 Comment

Thanks. I can at least blame the PHP documentation partly for my stupidity :/
0

You added months, not minutes. Try i instead of m

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.