

On the other hand, the %a character sequence returns the total number of days that have passed between two dates. Their value will never be greater than 31. The %D and %d character sequences return the number of days between two dates after subtracting the number of months and years. The days returned by %D or %d and %a are different. You just have to pass the %a character sequence to the format() method. Once you have computed the difference between two dates using the diff() method, getting the number of days between them is easy. The DateTime class takes care of everything for you like different number of days in different months, leap years and daylight saving time. Output: 08 years 01 months and 24 days 06 hours 43 minutes and 38 seconds. Output: 34 years 02 months and 09 days 00 hours 00 minutes and 00 seconds.Įcho $interval->format('%Y years %M months and %D days %H hours %I minutes and %S seconds.')


$interval = $first_date->diff($second_date) r: This will output “-” when negative and nothing when positive.Įxample: Calculated the number of years, months and days between two dates.R: This will output “-” when negative and “+” when positive.Single digits will be followed by leading zero. f: This will output microseconds numerically with at least 6 digits.F: This will output microseconds numerically with at least 6 digits.s: This will output seconds numerically without any leading zeroes.S: This will output seconds numerically with at least 2 digits.i: This will output minutes numerically without any leading zeroes.I: This will output minutes numerically with at least 2 digits.h: This will output hours numerically without any leading zeroes.H: This will output hours numerically with at least 2 digits.a: This will output the total number of days computed by DateTime::diff() or (unknown) otherwise.d: This will output days numerically without any leading zeroes.

