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格式化参数:
formatcharacter | Description | Example values |
|---|---|---|
| % | Literal % | % |
| Y | Years, numeric, at least 2 digits with leading 0 | 01, 03 |
| y | Years, numeric | 1, 3 |
| M | Months, numeric, at least 2 digits with leading 0 | 01, 03, 12 |
| m | Months, numeric | 1, 3, 12 |
| D | Days, numeric, at least 2 digits with leading 0 | 01, 03, 31 |
| d | Days, numeric | 1, 3, 31 |
| a | Total number of days as a result of a DateTime::diff() or (unknown)otherwise | 4, 18, 8123 |
| H | Hours, numeric, at least 2 digits with leading 0 | 01, 03, 23 |
| h | Hours, numeric | 1, 3, 23 |
| I | Minutes, numeric, at least 2 digits with leading 0 | 01, 03, 59 |
| i | Minutes, numeric | 1, 3, 59 |
| S | Seconds, numeric, at least 2 digits with leading 0 | 01, 03, 57 |
| s | Seconds, numeric | 1, 3, 57 |
| R | Sign "-" when negative, "+" when positive | -, + |
| r | Sign "-" when negative, empty when positive | -, |

浙公网安备 33010602011771号