简体   繁体   中英

PHP echo the name of the previous month e.g. June

Currently I'm using <?php echo date('F', strtotime('-1 month')); ?> <?php echo date('F', strtotime('-1 month')); ?> , which has been working fine but today it the 31st of July, but there are only 30 days in June.

For some reason echo date('F', strtotime('-1 month')) and echo date('F') return the same month eg July. Then echo date('F', strtotime('-2 month')) and echo date('F', strtotime('-3 month')) return May and so on.

Check this. This will give you previous Month Name

<?php

  $pp = date('m', strtotime(date('Y-m')." -1 month"));
  echo date('F', mktime(0, 0, 0, $pp, 10));
?>

This should work no matter how many days there are in the month.

 <?php // demo/temp_grs.php /** * Previous Month * * https://stackoverflow.com/questions/45421749/php-echo-the-name-of-the-previous-month-eg-june */ error_reporting(E_ALL); echo '<pre>'; $start = date('Ym-1') . '- 1 day'; $month = date('F', strtotime($start)); echo $month; 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM