I would like to get an array containing strings like this: 2013-03, 2013-02, 2013-01, 2012-12, 2012-11... etc.
How would I go about it? I tried working with a loop and DateInterval class, but surprisingly 30.March -1month gives us 2.March, so that's not very useful.
$date = new DateTime();
$date->sub(DateInterval::createFromDateString('1 month'));
Thanks!
Curiously, PHP returns the previous month of a date using the "next month" keyword :S
You can do the following:
<?php
$date = new DateTime();
// Subtract one month at a time
// until we reach 2009.
while ($date->format('Y') > 2009) {
$date->sub(DateInterval::createFromDateString('next month'));
var_dump($date->format('Y-m')); // Will print 2013-03 2013-02 [...]
};
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.