Vpp Man
Vpp Man

Reputation: 2546

PHP DateTime format not working properly

I use this code:

$today_date = new DateTime('now');
$final_date = new DateTime('2012-03-22 09:00');
$interval = $today_date->diff($final_date);

$time_left = $interval->format('%d:%H:%i:%s');

I want $time_left to have the format dd:hh:mm:ss format (d-> day, h-> hour, m-> minute, s-> seconds).

But when i used %d:%H:%i:%s as format, it sometimes displays single number, that is without 0 padding for minute. For example: 11:14:7:45. Expected result: 11:14:07:45

Any mistakes ?

Upvotes: 0

Views: 221

Answers (2)

Jim H.
Jim H.

Reputation: 5579

Per DateInterval, %i is "Minutes, numeric: 1, 3, 59". Try %I which uses leading zeroes.

Upvotes: 1

hakre
hakre

Reputation: 197554

More a comment: Don't confuse the format of DateInterval with format of DateTime: i is Minutes, numeric, e.g. 1, 3, 59. In case you run into a problem or you use a function you don't know in and out, please consult the manual first.

Upvotes: 2

Related Questions