How to get the time of last week, this week, last month, this month, this quarter and last quarter in PHP

In the development process, the commonly used date processing functions, timestamp processing functions, etc. are explained as follows:
<?php
echo date('Y-m-d h:i:s',time());

//Running result (year, month, day, hour, minute, second, "-" interval): 2014-09-12 06:28:32

echo date('Y-m-d',time());

//Running result (year month day, "-" interval): 2014-09-12

echo date('Y-m-d',strtotime(date('Y-m-d', time()-86400)));

//Running result (year, month, day of the day before the current date, "-" interval): 2014-09-11

echo date('Ymd',time());

//Running result (year month day, no interval): 20140912

echo date('m-d',time());

//Running result (month day, "-" interval): 09-12

echo str_replace("-","月",date('m-d',time()-date('w',time())*86400))."日";

//Running result (month and day, Chinese character display interval): September 12

echo date('w',time());

//Run result (day of week): 5

echo time();

//Running result (seconds of current date and time): 1410503809

echo strtotime(date('Y-m-d',time()));

//Running result (seconds of current date, specific to day): 1410503809

echo date('Y-m-d',strtotime(date('Y-m-d', time()))-date('w',strtotime(date('Y-m-d', time())))*86400);

//Running result (the starting date of the natural week to which the current date belongs is the date of Sunday, specific to the day, "-" interval): 2014-09-07
//php get today's start timestamp and end timestamp

$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));

$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;

//php get yesterday's start timestamp and end timestamp

$beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));

$endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;

//php get the start timestamp and end timestamp of last week

$beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));

$endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));

//php get the start timestamp and end timestamp of this month

$beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));

$endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));

<?php
    /**date 2015-04-11
     * author http://www.lai18.com
     **/
    echo date("Ymd",strtotime("now")), "\n";
    echo date("Ymd",strtotime("-1 week Monday")), "\n";
    echo date("Ymd",strtotime("-1 week Sunday")), "\n";
    echo date("Ymd",strtotime("+0 week Monday")), "\n";
    echo date("Ymd",strtotime("+0 week Sunday")), "\n";

    echo "*********Month month:";
    echo date('n');
    echo "*********Day of the week:";
    echo date("w");
    echo "*********Days in this month:";
    echo date("t");
    echo "*********";

    echo '<br>Starting time of last week:<br>';
    echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y"))),"\n";
    echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y"))),"\n";
    echo '<br>Start of this week:<br>';
    echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"))),"\n";
    echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"))),"\n";

    echo '<br>Start time of last month:<br>';
    echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y"))),"\n";
    echo date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y"))),"\n";
    echo '<br>Start of this month:<br>';
    echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y"))),"\n";
    echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y"))),"\n";

    $season = ceil((date('n'))/3);//What quarter is the month
    echo '<br>Start of this quarter:<br>';
    echo date('Y-m-d H:i:s', mktime(0, 0, 0,$season*3-3+1,1,date('Y'))),"\n";
    echo date('Y-m-d H:i:s', mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'))),"\n";

    $season = ceil((date('n'))/3)-1;//What quarter is the last quarter
    echo '<br>Start time of last quarter:<br>';
    echo date('Y-m-d H:i:s', mktime(0, 0, 0,$season*3-3+1,1,date('Y'))),"\n";
    echo date('Y-m-d H:i:s', mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'))),"\n";
?>


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324722700&siteId=291194637
Recommended