PHP获取今天,本年度,本周,上周,本月,上月,上季度,本季度,等各时间方法汇总

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

    echo '<br>上周起始时间:<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>本周起始时间:<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>上月起始时间:<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>本月起始时间:<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);//当月是第几季度
    echo '<br>本季度起始时间:<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;//上季度是第几季度
    echo '<br>上季度起始时间:<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";

    echo '<br>本年度起止时间:<br>';
    echo date('Y-m-d',strtotime(date("Y",time())."-1"."-1")); //本年开始
    echo date('Y-m-d',strtotime(date("Y",time())."-12"."-31")); //本年结束

 

 

 

 

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

$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获取昨日起始时间戳和结束时间戳

$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获取上周起始时间戳和结束时间戳

$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获取本月起始时间戳和结束时间戳

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

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

 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

echo time();

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

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

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

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

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

 

 

原地址:https://www.cnblogs.com/yangzailu/p/6474600.html

谢谢~

posted @ 2019-09-28 09:06  遇事稳坐钓鱼台  阅读(2818)  评论(0编辑  收藏  举报