PHP strtotime +1 month behaviour

前端 未结 8 2187
再見小時候
再見小時候 2020-11-30 10:10

I know about the unwanted behaviour of PHP\'s function

strtotime

For example, when adding a month (+1

相关标签:
8条回答
  • 2020-11-30 10:49

    Here's the algorithm you can use. It should be simple enough to implement yourself.

    • Have the original date and the +1 month date in variables
    • Extract the month part of both variables
    • If the difference is greater than 1 month (or if the original is December and the other is not January) change the latter variable to the last day of the next month. You can use for example t in date() to get the last day: date( 't.m.Y' )
    0 讨论(0)
  • 2020-11-30 10:53
    function ldom($m,$y){
    
         //return tha last date of a given month based on the month and the year 
        //(factors in leap years)
    
        $first_day= strtotime (date($m.'/1/'.$y));
        $next_month = date('m',strtotime ( '+32 day' , $first_day)) ;   
        $last_day= strtotime ( '-1 day' , strtotime (date($next_month.'/1/'.$y)) ) ;
        return $last_day;       
    
    }
    
    0 讨论(0)
提交回复
热议问题