Reputation: 33
When I attempt to format my date difference using the normal PHP codes (d,Y,m etc.) for dates and times, it simply outputs the letter, instead of the value. This is only when I format a DateTime::diff. It works fine with a simply DateTime object.
This:
$date1 = new DateTime('2000-01-01');
$date2= new DateTime('now');
$date=$date2->diff($date1);
echo $date->format('d days ago');
Outputs "d days ago".
I know that if I replace the d with a %a, it will output how many days ago this was. I was wondering what were the other characters that would output seconds, minutes, or even years.
Thanks in advance!
Upvotes: 3
Views: 5641
Reputation: 395
DateTime::diff() returns a DateInterval object.
For example:
<?php
$date1 = new DateTime('2000-01-01');
$date2= new DateTime('now');
$interval=$date2->diff($date1);
echo "Years: {$interval->y }\n";
echo "Months: {$interval->m }\n";
echo "Days: {$interval->d }\n";
echo "Hours: {$interval->h }\n";
echo "Mins: {$interval->i }\n";
echo "Secs: {$interval->s }\n";
echo $interval->format("%Y years, %m months, %d days, %H hours, %i minutes, %s seconds") . "\n";
Will output:
Years: 13
Months: 1
Days: 11
Hours: 13
Mins: 14
Secs: 44
13 years, 1 months, 11 days, 13 hours, 21 minutes, 43 seconds
Upvotes: 8