php獲取當前月月初至月末的時間戳,上個月月初至月末的時間戳


當前月

<?php
$thismonth = date('m');
$thisyear  = date('Y');
$startDay = $thisyear . '-' . $thismonth . '-1';
$endDay   = $thisyear . '-' . $thismonth . '-' . date('t', strtotime($startDay));
$b_time       = strtotime($startDay);
$e_time       = strtotime($endDay);

 

上一月

<?php
$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);

這里對關鍵的就是date函數中的t,它是用來獲取當前月所含天數的,28天,29天,30天,31天。含有多少天,月底就是多少號。


免責聲明!

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



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