Search code examples
phpdateunix-timestamp

PHP get last month name from a timestamp


I have a Unix timestamp and would like to get the name of the preceding month e. g. "Ferbruary"

$date = 1489842000;
$lastMonth = getLastMonth($date); //Ferbruary

Solution

  • strtotime is your friend here:

    echo Date('F', strtotime($date . " last month"));
    

    For anyone that wants this fully dynamic, to always display last month's name, the code would be:

    $currentMonth = date('F');
    echo Date('F', strtotime($currentMonth . " last month"));