"; echo"昨天:",date("Y-m-d",strtotime("-1day")),"
"; echo"明天:",date("Y-m-d",strtotime("+1day")),"
"; echo"一周后:",date("Y-m-d",strtotime("+1week")),"
"; echo"一周零两天四小时两秒后:",date("Y-m-dG:H:s",strtotime("+1week2days4hours2seconds")),"
"; echo"下个星期四:",date("Y-m-d",strtotime("nextThursday")),"
"; echo"上个周一:".date("Y-m-d",strtotime("lastMonday"))."
"; echo"一个月前:".date("Y-m-d",strtotime("lastmonth"))."
"; echo"一个月后:".date("Y-m-d",strtotime("+1month"))."
"; echo"十年后:".date("Y-m-d",strtotime("+10year"))."
";

运行结果:

2018-12-26今天:2018-12-21
--------------------------------------------------------------------------------
昨天:2018-12-20
--------------------------------------------------------------------------------
明天:2018-12-22
--------------------------------------------------------------------------------
一周后:2018-12-28
--------------------------------------------------------------------------------
一周零两天四小时两秒后:2018-12-3015:15:46
--------------------------------------------------------------------------------
下个星期四:2018-12-27
--------------------------------------------------------------------------------
上个周一:2018-12-17
--------------------------------------------------------------------------------
一个月前:2018-11-21
--------------------------------------------------------------------------------
一个月后:2019-01-21
--------------------------------------------------------------------------------
十年后:2028-12-21
--------------------------------------------------------------------------------

php本周开始时间和结束时间;本月开始时间结束时间;上月开始时间结束时间

date_default_timezone_set('PRC');//默认时区
/**
*功能:取得给定日期所在周的开始日期和结束日期
*参数:$gdate日期,默认为当天,格式:YYYY-MM-DD
*$first一周以星期一还是星期天开始,0为星期天,1为星期一
*返回:数组array("开始日期","结束日期");
*
*/
functionaweek($gdate="",$first=0){
if(!$gdate)$gdate=date("Y-m-d");
$w=date("w",strtotime($gdate));//取得一周的第几天,星期天开始0-6
$dn=$w?$w-$first:6;//要减去的天数
//本周开始日期
$st=date("Y-m-d",strtotime("$gdate-".$dn."days"));
//本周结束日期
$en=date("Y-m-d",strtotime("$st+6days"));
//上周开始日期
$last_st=date('Y-m-d',strtotime("$st-7days"));
//上周结束日期
$last_en=date('Y-m-d',strtotime("$st-1days"));
returnarray($st,$en,$last_st,$last_en);//返回开始和结束日期
}
echoimplode("|",aweek("",1)).'
'; //echodate("Y-m-d",strtotime("time()")); echo'本周第一天(星期日为一周开始):'.date('Y-m-d',time()-86400*date('w')).'
'; echo'本周第一天(星期一为一周开始):'.date('Y-m-d',time()-86400*date('w')+(date('w')>0?86400:-6*86400)).'
'; echo'本月第一天:'.date('Y-m-d',mktime(0,0,0,date('m'),1,date('Y'))).'
'; echo'本月最后一天:'.date('Y-m-d',mktime(0,0,0,date('m'),date('t'),date('Y'))).'
'; //上个月的开始日期 $m=date('Y-m-d',mktime(0,0,0,date('m')-1,1,date('Y'))); //上个月共多少天 $t=date('t',strtotime("$m")); echo'上月第一天:'.date('Y-m-d',mktime(0,0,0,date('m')-1,1,date('Y'))).'
'; echo'上月最后一天:'.date('Y-m-d',mktime(0,0,0,date('m')-1,$t,date('Y'))).'
';

运行结果:

2018-12-17|2018-12-23|2018-12-10|2018-12-16
本周第一天(星期日为一周开始):2018-12-16
--------------------------------------------------------------------------------
本周第一天(星期一为一周开始):2018-12-17
--------------------------------------------------------------------------------
本月第一天:2018-12-01
--------------------------------------------------------------------------------
本月最后一天:2018-12-31
--------------------------------------------------------------------------------
上月第一天:2018-11-01
--------------------------------------------------------------------------------
上月最后一天:2018-11-30
--------------------------------------------------------------------------------

//PHP手册上有一个这个方法,用来返回指定日期的周一和周日
functionget_week_range($week,$year){
$timestamp=mktime(1,0,0,1,1,$year);
$firstday=date("N",$timestamp);
if($firstday>4){
$firstweek=strtotime('+'.(8-$firstday).'days',$timestamp);
}else{
$firstweek=strtotime('-'.($firstday-1).'days',$timestamp);
}
$monday=strtotime('+'.($week-1).'week',$firstweek);
$sunday=strtotime('+6days',$monday);
$start=date("Y-m-d",$monday);
$end=date("Y-m-d",$sunday);
returnarray($start,$end);
}
//strtotime获取本周第一天和最后一天方法的BUG
//PHP手册上有一个这个方法,用来返回指定日期的周一和周日
functionget_week_range2($week,$year){
$timestamp=mktime(1,0,0,1,1,$year);
$firstday=date("N",$timestamp);
if($firstday>4){
$firstweek=strtotime('+'.(8-$firstday).'days',$timestamp);
}else{
$firstweek=strtotime('-'.($firstday-1).'days',$timestamp);
}
$monday=strtotime('+'.($week-1).'week',$firstweek);
$sunday=strtotime('+6days',$monday);
$start=date("Y-m-d",$monday);
$end=date("Y-m-d",$sunday);
returnarray($start,$end);
}

但在跨年的时候使用会有问题

例如2011年的12月31日周六和2012年1月1日周日,拿到的周一和周日完全不同

2011年12月31日拿合到的周一和周日分别对应
2011-12-26
2012-01-01

但2012年1月1日拿到的周一和周日分别对应

2012-01-02
2012-01-04

原因为传进去的方法的周为第53周,但是年为2011年,所以认为2011的第53周,所以计算有误,解决方法为,

如果周为大于10(因为一月个月不可能有10周),且月份为1的时候,将年减1处理

if(date('m',$last_week_time)=='01'and$tmp_last_week>10){
$last_week_year--;
}

PS:这里再为大家推荐几款时间及日期相关工具供大家参考:

在线日期/天数计算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi

在线日期计算器/相差天数计算器:
http://tools.jb51.net/jisuanqi/datecalc

在线日期天数差计算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq

Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php日期与时间用法总结》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

热门推荐

1 虎年新年专属祝福语简短
2 恋爱很久的祝福语简短
3 单位搬迁新楼祝福语简短
4 新年祝福语给婆婆简短
5 简短媳妇的生日祝福语
6 相恋人回去祝福语简短
7 新人给朋友祝福语简短
8 结婚给姐妹祝福语简短
9 结婚对白誓言简短祝福语
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短