PHP獲取本月起始和終止時間戳


一、本月起始和結束

//獲取本月開始的時間戳
$beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
//獲取本月結束的時間戳
$endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
View Code

二、上月起始和結束

     寫法一:

$m = date('Y-m-d', mktime(0,0,0,date('m')-1,1,date('Y'))); //上個月的開始日期
        $t = date('t',strtotime($m)); //上個月共多少天

        $start = date('Y-m-d', mktime(0,0,0,date('m')-1,1,date('Y'))); //上個月的開始日期
        $end = date('Y-m-d', mktime(0,0,0,date('m')-1,$t,date('Y'))); //上個月的結束日期
        //echo 15*24*3600;
        //echo 30*24*3600;

        $time=strtotime($start);
        dump(date('Y-m-d H:i:s',$time));//2017-06-01 00:00:00
        $jieshu=strtotime($end);
        dump(date('Y-m-d H:i:s',$jieshu));//2017-06-30 00:00:00

 

     寫法二:

 

$thismonth = date('m');
        $thisyear = date('Y');
        if ($thismonth == 1) {
         $lastmonth = 12;
         $lastyear = $thisyear - 1;
        } else {
         $lastmonth = $thismonth - 1;
         $lastyear = $thisyear;
        }
        $lastStartDay = $lastyear . '-' . $lastmonth . '-1';
        $lastEndDay = $lastyear . '-' . $lastmonth . '-' . date('t', strtotime($lastStartDay));
        $b_time = strtotime($lastStartDay);//上個月的月初時間戳
        $e_time = strtotime($lastEndDay);//上個月的月末時間戳2017-06-30 00:00:00(注意 是最后一天的開始時間點)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM