Home  >  Article  >  Backend Development  >  PHP method to get last week, this week, last month, this month, this quarter, and last quarter time

PHP method to get last week, this week, last month, this month, this quarter, and last quarter time

不言
不言Original
2018-04-18 13:49:139681browse

The content of this article is about the method of obtaining last week, this week, last month, this month, this quarter, and last quarter in PHP. It has certain reference value. Now I share it with you. Friends in need can refer to it. For a moment

<br/>
<br/>
开发过程中,常用日期处理函数,时间戳处理函数等,如下讲解:
<?php
echo date(&#39;Y-m-d h:i:s&#39;,time());

//运行结果(年月日时分秒,“-”间隔):2014-09-12 06:28:32

echo date(&#39;Y-m-d&#39;,time());

//运行结果(年月日,“-”间隔):2014-09-12

echo date(&#39;Y-m-d&#39;,strtotime(date(&#39;Y-m-d&#39;, time()-86400)));

//运行结果(当前日期前一天的年月日,“-”间隔):2014-09-11

echo date(&#39;Ymd&#39;,time());

//运行结果(年月日,无间隔):20140912

echo date(&#39;m-d&#39;,time());

//运行结果(月日,“-”间隔):09-12

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

//运行结果(月日,汉字显示间隔):09月12日

echo date(&#39;w&#39;,time());

//运行结果(星期几):5

echo time();

//运行结果(当前日期时间的秒数):1410503809

echo strtotime(date(&#39;Y-m-d&#39;,time()));

//运行结果(当前日期秒数,具体到天):1410503809

echo date(&#39;Y-m-d&#39;,strtotime(date(&#39;Y-m-d&#39;, time()))-date(&#39;w&#39;,strtotime(date(&#39;Y-m-d&#39;, time())))*86400);

//运行结果(当前日期所属自然周的起始日期即周日的日期,具体到天,“-”间隔):2014-09-07



//php获取今日开始时间戳和结束时间戳

$beginToday=mktime(0,0,0,date(&#39;m&#39;),date(&#39;d&#39;),date(&#39;Y&#39;));

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

//php获取昨日起始时间戳和结束时间戳

$beginYesterday=mktime(0,0,0,date(&#39;m&#39;),date(&#39;d&#39;)-1,date(&#39;Y&#39;));

$endYesterday=mktime(0,0,0,date(&#39;m&#39;),date(&#39;d&#39;),date(&#39;Y&#39;))-1;

//php获取上周起始时间戳和结束时间戳

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

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

//php获取本月起始时间戳和结束时间戳

$beginThismonth=mktime(0,0,0,date(&#39;m&#39;),1,date(&#39;Y&#39;));

$endThismonth=mktime(23,59,59,date(&#39;m&#39;),date(&#39;t&#39;),date(&#39;Y&#39;));


<?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 "*********第几个月:";
    echo date(&#39;n&#39;);
    echo "*********本周周几:";
    echo date("w");
    echo "*********本月天数:";
    echo date("t");
    echo "*********";

    echo &#39;<br>上周起始时间:<br>&#39;;
    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 &#39;<br>本周起始时间:<br>&#39;;
    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 &#39;<br>上月起始时间:<br>&#39;;
    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 &#39;<br>本月起始时间:<br>&#39;;
    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(&#39;n&#39;))/3);//当月是第几季度
    echo &#39;<br>本季度起始时间:<br>&#39;;
    echo date(&#39;Y-m-d H:i:s&#39;, mktime(0, 0, 0,$season*3-3+1,1,date(&#39;Y&#39;))),"\n";
    echo date(&#39;Y-m-d H:i:s&#39;, mktime(23,59,59,$season*3,date(&#39;t&#39;,mktime(0, 0 , 0,$season*3,1,date("Y"))),date(&#39;Y&#39;))),"\n";

    $season = ceil((date(&#39;n&#39;))/3)-1;//上季度是第几季度
    echo &#39;<br>上季度起始时间:<br>&#39;;
    echo date(&#39;Y-m-d H:i:s&#39;, mktime(0, 0, 0,$season*3-3+1,1,date(&#39;Y&#39;))),"\n";
    echo date(&#39;Y-m-d H:i:s&#39;, mktime(23,59,59,$season*3,date(&#39;t&#39;,mktime(0, 0 , 0,$season*3,1,date("Y"))),date(&#39;Y&#39;))),"\n";
?>


The above is the detailed content of PHP method to get last week, this week, last month, this month, this quarter, and last quarter time. For more information, please follow other related articles on the PHP Chinese website!

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