php获取本周周一、周日时间,上周周一、周日时间,本月第一天,本月最后一天,上个月第一天,最后一天时间
转自csdn
//这个星期的星期一 // @$timestamp ,某个星期的某一个时间戳,默认为当前时间 // @is_return_timestamp ,是否返回时间戳,否则返回时间格式 function this_monday($timestamp=0,$is_return_timestamp=true){ static $cache ; $id = $timestamp.$is_return_timestamp; if(!isset($cache[$id])){ if(!$timestamp) $timestamp = time(); $monday_date = date('Y-m-d', $timestamp-86400*date('w',$timestamp)+(date('w',$timestamp)>0?86400:-/*6*86400*/518400)); if($is_return_timestamp){ $cache[$id] = strtotime($monday_date); }else{ $cache[$id] = $monday_date; } } return $cache[$id]; } //这个星期的星期天 // @$timestamp ,某个星期的某一个时间戳,默认为当前时间 // @is_return_timestamp ,是否返回时间戳,否则返回时间格式 function this_sunday($timestamp=0,$is_return_timestamp=true){ static $cache ; $id = $timestamp.$is_return_timestamp; if(!isset($cache[$id])){ if(!$timestamp) $timestamp = time(); $sunday = this_monday($timestamp) + /*6*86400*/518400; if($is_return_timestamp){ $cache[$id] = $sunday; }else{ $cache[$id] = date('Y-m-d',$sunday); } } return $cache[$id]; } //上周一 // @$timestamp ,某个星期的某一个时间戳,默认为当前时间 // @is_return_timestamp ,是否返回时间戳,否则返回时间格式 function last_monday($timestamp=0,$is_return_timestamp=true){ static $cache ; $id = $timestamp.$is_return_timestamp; if(!isset($cache[$id])){ if(!$timestamp) $timestamp = time(); $thismonday = this_monday($timestamp) - /*7*86400*/604800; if($is_return_timestamp){ $cache[$id] = $thismonday; }else{ $cache[$id] = date('Y-m-d',$thismonday); } } return $cache[$id]; } //上个星期天 // @$timestamp ,某个星期的某一个时间戳,默认为当前时间 // @is_return_timestamp ,是否返回时间戳,否则返回时间格式 function last_sunday($timestamp=0,$is_return_timestamp=true){ static $cache ; $id = $timestamp.$is_return_timestamp; if(!isset($cache[$id])){ if(!$timestamp) $timestamp = time(); $thissunday = this_sunday($timestamp) - /*7*86400*/604800; if($is_return_timestamp){ $cache[$id] = $thissunday; }else{ $cache[$id] = date('Y-m-d',$thissunday); } } return $cache[$id]; } //这个月的第一天 // @$timestamp ,某个月的某一个时间戳,默认为当前时间 // @is_return_timestamp ,是否返回时间戳,否则返回时间格式 function month_firstday($timestamp = 0, $is_return_timestamp=true){ static $cache ; $id = $timestamp.$is_return_timestamp; if(!isset($cache[$id])){ if(!$timestamp) $timestamp = time(); $firstday = date('Y-m-d', mktime(0,0,0,date('m',$timestamp),1,date('Y',$timestamp))); if($is_return_timestamp){ $cache[$id] = strtotime($firstday); }else{ $cache[$id] = $firstday; } } return $cache[$id]; } //这个月的第一天 // @$timestamp ,某个月的某一个时间戳,默认为当前时间 // @is_return_timestamp ,是否返回时间戳,否则返回时间格式 function month_lastday($timestamp = 0, $is_return_timestamp=true){ static $cache ; $id = $timestamp.$is_return_timestamp; if(!isset($cache[$id])){ if(!$timestamp) $timestamp = time(); $lastday = date('Y-m-d', mktime(0,0,0,date('m',$timestamp),date('t',$timestamp),date('Y',$timestamp))); if($is_return_timestamp){ $cache[$id] = strtotime($lastday); }else{ $cache[$id] = $lastday; } } return $cache[$id]; } //上个月的第一天 // @$timestamp ,某个月的某一个时间戳,默认为当前时间 // @is_return_timestamp ,是否返回时间戳,否则返回时间格式 function lastmonth_firstday($timestamp = 0, $is_return_timestamp=true){ static $cache ; $id = $timestamp.$is_return_timestamp; if(!isset($cache[$id])){ if(!$timestamp) $timestamp = time(); $firstday = date('Y-m-d', mktime(0,0,0,date('m',$timestamp)-1,1,date('Y',$timestamp))); if($is_return_timestamp){ $cache[$id] = strtotime($firstday); }else{ $cache[$id] = $firstday; } } return $cache[$id]; } //上个月的第一天 // @$timestamp ,某个月的某一个时间戳,默认为当前时间 // @is_return_timestamp ,是否返回时间戳,否则返回时间格式 function lastmonth_lastday($timestamp = 0, $is_return_timestamp=true){ static $cache ; $id = $timestamp.$is_return_timestamp; if(!isset($cache[$id])){ if(!$timestamp) $timestamp = time(); $lastday = date('Y-m-d', mktime(0,0,0,date('m',$timestamp)-1, date('t',lastmonth_firstday($timestamp)),date('Y',$timestamp))); if($is_return_timestamp){ $cache[$id] = strtotime($lastday); }else{ $cache[$id] = $lastday; } } return $cache[$id]; } echo '本周星期一:'.this_monday(0,false).''; echo '本周星期天:'.this_sunday(0,false).''; echo '上周星期一:'.last_monday(0,false).''; echo '上周星期天:'.last_sunday(0,false).''; echo '本月第一天:'.month_firstday(0,false).''; echo '本月最后一天:'.month_lastday(0,false).''; echo '上月第一天:'.lastmonth_firstday(0,false).''; echo '上月最后一天:'.lastmonth_lastday(0,false).'';
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧