php 获取日期(今天、昨天、上周、本周、上月、本月、季度、去年)

2026-01-16 14:33:07

获取日期有很多种写法,“这”只是其中一种,欢迎指出错误并补充:

//今天

$today = date("Y-m-d");

//昨天

$yesterday = date("Y-m-d",strtotime("-1 day"));

//上周

$lastweek_start = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y")));

$lastweek_end = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y")));

//本周

$thisweek_start = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")));

$thisweek_end = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")));

//上月

/*

*不太靠谱,当上个月没有当前天的日期会出错,比如10月31日时获取上个月月初和月末就会变成10月1日和10月31日

*同理,+1 moth也是一样

*/

$lastmonth_start = date('Y-m-01', strtotime('-1 month'));

$lastmonth_start = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y")));// 这个才是王道

// 不太靠谱,同上

$lastmonth_end = date('Y-m-t', strtotime('-1 month'));

$lastmonth_end = date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y"))); // 这个才是王道

补充:--------------------突然看到这个也很有意思-----------------------------

上个月第一天:echo date('Y-m-d', strtotime(date('Y-m-01') . ' -1 month')); // 本月第一天再减一个月上个月最后一天:echo date('Y-m-d', strtotime(date('Y-m-01') . ' -1 day')); // 本月第一天再减一天

//本月

$thismonth_start = date("Y-m-01");

$thismonth_end = date("Y-m-t");

//本季度未最后一月天数

$getMonthDays = date("t",mktime(0, 0 , 0,date('n')+(date('n')-1)%3,1,date("Y")));

//本季度/

$thisquarter_start = date('Y-m-d H:i:s', mktime(0, 0, 0,date('n')-(date('n')-1)%3,1,date('Y')));

$thisquarter_end = date('Y-m-d H:i:s', mktime(23,59,59,date('n')+(date('n')-1)%3,$getMonthDays,date('Y')));

$season = ceil((date('n'))/3);//当月是第几季度$thisquarter_start = date('Y-m-d', mktime(0, 0, 0,$season*3-3+1,1,date('Y')));$thisquarter_end = 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')));

// 上季度

$season = ceil((date('n'))/3)-1;//上季度是第几季度$lastquarter_start = date('Y-m-d H:i:s', mktime(0, 0, 0,$season*3-3+1,1,date('Y')));$lastquarter_end = 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')));

// 去年

$lastyear_start = date("Y-01-01", strtotime('-1 year'));

$lastyear_end = date("Y-12-31", strtotime('-1 year'));

//2019-10-10这天 2个月后的日期

date("Y-m-d",strtotime("+2 month",strtotime("2019-10-10")));// 2019-12-10这个正常没问题

date("Y-m-d",strtotime("+1 month",strtotime("2019-10-31")));// 2019-12-01这个异常了,为啥呢?因为11月份没有31天。Oh,yeah.

//当前 4个月后的日期

date("Y-m-d",strtotime("+3 month",time()));

但是如果当前是2019-10-31,那么打印的结果是2020-03-02

//获取指定月份的月初和月末

//两种方法,第一种比较绕一点

$month_start =strtotime($return_time) ;//指定年月份月初时间戳 $BeginDate=date('Y-m-01', strtotime($return_time)); $EndDate = date('Y-m-d', strtotime("$BeginDate +1 month -1 day")); $month_end= strtotime("next day", strtotime($EndDate)) - 1;//指定年月份月末时间戳 $months=$return_time.'--'.$month_start .' and '.$month_end;

//第二种

时间戳:

$return_time_begin = strtotime($return_time) ; $return_time_end = strtotime(date('Y-m-t', $return_time_begin));

日期:

$return_time_begin = strtotime($return_time) ;

$return_date_end = date('Y-m-01', $return_time_begin);或$return_date_end = $return_time.'-01'; $return_date_end = date('Y-m-t', $return_time_begin);

    武磊:10年前我是世界杯护旗手 这次希望站上赛场
    如何在Win11/10中卸载和重置Minecraft应用程序

    Copyright © 2022 摩洛哥世界杯_直播世界杯决赛 - dgaida.com All Rights Reserved.