php中DateTime、diff

  

手册地址:http://php.net/manual/en/dateinterval.format.php

 1 $january = new DateTime('2010-01-01');
 2 $february = new DateTime('2010-02-01');
 3 $interval = $january->diff($february);
 4 
 5 // %a will output the total number of days.
 6 echo $interval->format('%R%a total days'). '<br/>';// +31 total days
 7 
 8 // While %d will only output the number of days not already covered by the
 9 // month.
10 echo $interval->format('%m month, %d days');// 1 month, 0 days

format格式化参数:


The following characters are recognized in the format parameter string. Each format character must be prefixed by a percent sign (%).
formatcharacterDescriptionExample values
% Literal % %
Y Years, numeric, at least 2 digits with leading 0 0103
y Years, numeric 13
M Months, numeric, at least 2 digits with leading 0 010312
m Months, numeric 1312
D Days, numeric, at least 2 digits with leading 0 010331
d Days, numeric 1331
a Total number of days as a result of a DateTime::diff() or (unknown)otherwise 4188123
H Hours, numeric, at least 2 digits with leading 0 010323
h Hours, numeric 1323
I Minutes, numeric, at least 2 digits with leading 0 010359
i Minutes, numeric 1359
S Seconds, numeric, at least 2 digits with leading 0 010357
s Seconds, numeric 1357
R Sign "-" when negative, "+" when positive -+
r Sign "-" when negative, empty when positive -,
原文地址:https://www.cnblogs.com/firstForEver/p/5239773.html