- date: documentation ( source)
- strtotime: documentation ( source)
<?php
function getPrevMonthDatetime($datetime)
{
return date('Y-m-d H:i:s', strtotime('today of last month 00:00:00', strtotime($datetime)));
}
for ($datetime = '2016-01-01 00:00:00'; strtotime($datetime) < strtotime('2017-01-01 00:00:00'); $datetime = date('Y-m-d H:i:s', strtotime($datetime)+60*60*24)) {
echo $datetime.' -> '.getPrevMonthDatetime($datetime)."\n";
}