Home  >  Article  >  Backend Development  >  PHP code to implement the last day of the previous month and the last day of a certain month

PHP code to implement the last day of the previous month and the last day of a certain month

WBOY
WBOYOriginal
2016-07-25 08:59:571039browse
Use PHP to implement the code for displaying the last day of the previous month and the last day of a certain month. Friends in need can refer to it.

Example 1, display the last day of the previous month

<?php
    //显示上个月的最后一天  
    function G_L_M_Lastday($dateStr=''){  
    $time = time();  
    if($dateStr !='')  
       $time = strtotime($dateStr);   
        return date('Y-m-t', strtotime('last month', $time));  
    }  
      
    echo G_L_M_Lastday();    // 默认显示当前日期的上月最后一天  
    echo "<br>";  
    echo G_L_M_Lastday('2010-3-3'); // 显示给定日期的上月最后一天  
    echo "<hr>"; 
   //by http://bbs.it-home.org
?>

Example 2, display the last day of a certain month

<?php
    //某个月的最后一天  
    $lastday = mktime(0, 0, 0, 3, 0, 2011);  
    echo strftime("Last day in Feb 2011 is: %d", $lastday);//2月的最后一天  
    $lastday1 = mktime(0, 0, 0, 4, -31, 2000);  
    echo strftime("Last day in Feb 2000 is: %d", $lastday1);//2月的最后一天  
?>

Example 3, concise version

<?php
    $time = strtotime('last month');  
    echo date('Y-m-t', $time); //上个月的最后一天  
    echo date('Y-m-01', $time); //上个月的第一天
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn