PHP获取上周、本周、上月、本月、本季度、上季度时间方法大全

2017-12-28     浏览次数:

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

//运行结果(年月日时分秒,“-”间隔):2017-12-28 14:10:32

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

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

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

//运行结果(当前日期前一天的年月日,“-”间隔):2017-12-27

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

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

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

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

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

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

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

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

echo time();

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

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

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

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

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

//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("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";
?>
*转载请注明出处,更多资料尽在 吾爱编程
返回吾爱编程首页