PHP get last month name from timestamp
2 answers
You can set the object DateTime
to a specified timestamp, then subtract the interval 'P1M'
(one month), for example:
/**
* @param {int} $date unix timestamp
* @return string name of month
*/
function getLastMonth($date) {
// create new DateTime object and set its value
$datetime = new DateTime();
$datetime->setTimestamp($date);
// subtract P1M - one month
$datetime->sub(new DateInterval('P1M'));
// return date formatted to month name
return $datetime->format('F');
}
// Example of use
$date = 1489842000;
$lastMonth = getLastMonth($date);
-1
source to share