php获取上周、本周、上月、本月的起止日期

//mktime(时,分,秒,月,日,年)

//date(format,timestamp),其中format常用有如下选项:
Y - 年份的四位数表示
m - 月份的数字表示(从 01 到 12)
d - 一个月中的第几天(从 01 到 31)
H - 24 小时制,带前导零(00 到 23)
i - 分,带前导零(00 到 59)
s - 秒,带前导零(00 到 59)
w - 星期几的数字表示,0~6(0 表示 Sunday[星期日],6 表示 Saturday[星期六])
t - 给定月份中包含的天数,28~31
z - 一年中的第几天; 如: 0~365
其它详见:http://www.w3school.com.cn/php/func_date_date.asp

//举例:获取某个具体时间的天数
$t=date('t',strtotime('2018-04-04'));  或者  $t=date('t',strtotime('20180404'));

echo date("Y-m-d");//今天
echo date("Y-m-d",strtotime("-1 day"));//昨天     
echo date("Y-m-d",strtotime("+1 day"));//明天 
echo date("Y-m-d",strtotime("+3 week"));//3周后 
echo date("Y-m-d H:i:s",strtotime("+1 week 3 days 5 hours 2 seconds"));//一周零三天五小时两秒后      
echo date("Y-m-d",strtotime("next Thursday"));//下周四     
echo date("Y-m-d",strtotime("last Monday"));//上周一      
echo date("Y-m-d",strtotime("last month"));//一个月前     
echo date("Y-m-d",strtotime("+1 month"));//一个月后 
echo date("Y-m-d",strtotime("+10 year"));//十年后

//获取本周起始时间戳和结束时间戳
$curr = date("Y-m-d"); 
$w=date('w');//获取当前周的第几天 周日是 0 周一到周六是1-6  
$beginLastweek=strtotime("$curr -".($w ? $w-1 : 6).' days');//获取本周开始日期,如果$w是0是周日:-6天;其它:-1天    
$s=date('Y-m-d 00:00:00',$beginLastweek);
$e=date('Y-m-d 23:59:59',strtotime("$s +6 days"));
echo $s;echo "<hr>";echo $e;echo "<hr>";

//获取本月起始时间戳和结束时间戳
$beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
$endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));

=========================================================

//获取上周起始时间戳和结束时间戳
$curr = date("Y-m-d"); 
$w=date('w');//获取当前周的第几天 周日是 0 周一到周六是1-6  
$beginLastweek=strtotime('$curr -'.($w ? $w-1 : 6).' days');//获取本周开始日期,如果$w是0是周日:-6天;其它:$w-1天    
$cur_monday=date('Y-m-d 00:00:00',$beginLastweek);
$s=date('Y-m-d 23:59:59',strtotime("$cur_monday -7 days"));
$e=date('Y-m-d 23:59:59',strtotime("$s +6 days"));
echo $s;echo "<hr>";echo $e;echo "<hr>";

//获取上月起始时间戳和结束时间戳
// 法1:
mktime(0, 0 , 0,date("m")-1,1,date("Y"))
mktime(23,59,59,date("m") ,0,date("Y"))
// 法2:
$m=date('Y-m-d', mktime(0,0,0,date('m')-1,1,date('Y'))); //上个月的开始日期
$t=date('t',strtotime($m)); //上个月共多少天
$beginLastmonth=mktime(0,0,0,date('m')-1,1,date('Y'));  
$endLastmonth=mktime(23,59,59,date('m')-1,$t,date('Y')); 

调用 
$s=date('Y-m-d',$beginLastmonth);
$e=date('Y-m-d',$endLastmonth);
echo $s;echo "<hr>";echo $e;die; 

以下是Python的datetime实现本周上周本月上月起止日期的代码: ```python import datetime # 本周起止日期 now = datetime.datetime.now() start_of_week = now - datetime.timedelta(days=now.weekday()) end_of_week = start_of_week + datetime.timedelta(days=6) # 上周起止日期 start_of_last_week = start_of_week - datetime.timedelta(days=7) end_of_last_week = end_of_week - datetime.timedelta(days=7) # 本月起止日期 start_of_month = datetime.datetime(now.year, now.month, 1) if now.month == 12: end_of_month = datetime.datetime(now.year+1, 1, 1) - datetime.timedelta(days=1) else: end_of_month = datetime.datetime(now.year, now.month+1, 1) - datetime.timedelta(days=1) # 上月起止日期 last_month = now.month - 1 if now.month > 1 else 12 last_year = now.year - 1 if last_month == 12 else now.year start_of_last_month = datetime.datetime(last_year, last_month, 1) end_of_last_month = datetime.datetime(now.year, now.month, 1) - datetime.timedelta(days=1) # 输出起止日期 print("本周起止日期:", start_of_week.date(), "-", end_of_week.date()) print("上周起止日期:", start_of_last_week.date(), "-", end_of_last_week.date()) print("本月起止日期:", start_of_month.date(), "-", end_of_month.date()) print("上月起止日期:", start_of_last_month.date(), "-", end_of_last_month.date()) ``` 其中关键的方法是`datetime.timedelta()`表示时间差,例如`datetime.timedelta(days=7)`表示7天时间差。`datetime.datetime()`则表示一个具体的日期时间。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值