ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
\* 获取当前时间上一周的开始时间和结束时间; \* 可以通过配置一周的开始时间,默认为星期一; \* 使用方法,执行 extract(get\_lastweek()); \* 之后,可以直接使用 $beginLastweek $endLastweek 两个变量; \* 如果需要其他变量名称,可以修改参数 $begin 和 $end ,自行指定名称 ``` /** * 获取当前时间上一周的开始时间和结束时间; * 可以通过配置一周的开始时间,默认为星期一; * 使用方法,执行 extract(get_lastweek()); * 之后,可以直接使用 $beginLastweek $endLastweek 两个变量; * 如果需要其他变量名称,可以修改参数 $begin 和 $end ,自行指定名称 * * @param int $week_start_num 一周的开始时间,默认为周一 * @param int $now_time 当前时间时间戳,这里做一个当前时间方便测试该方法的正确性 * @param string $begin 上一周开始时间的变量名称; * @param string $end 上一周结束时间的变量名称; * * @author leeyi <leeyisoft@qq.com> * @return array() */ function get_lastweek($week_start_num = 1, $now_time = 0, $begin = 'beginLastweek', $end = 'endLastweek') { $now_time = $now_time>0 ? $now_time : time(); $now_weekday = date('w', $now_time); // 获取当前是星期前 0-6 星期日-星期六 $week_start_num = in_array($week_start_num, ['0','1','2','3','4','5','6']) ? $week_start_num : 1; // 默认一周开始时间为周一 $now_weekday = $now_weekday<$week_start_num ? $now_weekday+7 : $now_weekday; //php获取上周起始时间戳和结束时间戳 $beginLastweek = $now_time-($now_weekday+7-$week_start_num)*86400; $endLastweek = $beginLastweek+(6*86400); if( APP_DEBUG ) { // for test echo date('Y-m-d',$now_time).' : '.date('Y-m-d 00:00:00', $beginLastweek).' => '.date('Y-m-d 23:59:59', $endLastweek).'<br/>'; } return array( $begin => strtotime(date('Y-m-d 00:00:00', $beginLastweek)), $end => strtotime(date('Y-m-d 23:59:59', $endLastweek)) ); } // 测试案例 get_lastweek($week_start_num=0, strtotime('2015-03-22')); get_lastweek($week_start_num=0, strtotime('2015-03-23')); get_lastweek($week_start_num=0, strtotime('2015-03-24')); get_lastweek($week_start_num=0, strtotime('2015-03-25')); get_lastweek($week_start_num=0, strtotime('2015-03-26')); get_lastweek($week_start_num=0, strtotime('2015-03-27')); get_lastweek($week_start_num=0, strtotime('2015-03-28')); get_lastweek($week_start_num=0, strtotime('2015-03-29')); echo '<hr/>'; get_lastweek($week_start_num=1, strtotime('2015-03-22')); get_lastweek($week_start_num=1, strtotime('2015-03-23')); get_lastweek($week_start_num=1, strtotime('2015-03-24')); get_lastweek($week_start_num=1, strtotime('2015-03-25')); get_lastweek($week_start_num=1, strtotime('2015-03-26')); get_lastweek($week_start_num=1, strtotime('2015-03-27')); get_lastweek($week_start_num=1, strtotime('2015-03-28')); get_lastweek($week_start_num=1, strtotime('2015-03-29')); echo '<hr/>'; get_lastweek($week_start_num=2, strtotime('2015-03-22')); get_lastweek($week_start_num=2, strtotime('2015-03-23')); get_lastweek($week_start_num=2, strtotime('2015-03-24')); get_lastweek($week_start_num=2, strtotime('2015-03-25')); get_lastweek($week_start_num=2, strtotime('2015-03-26')); get_lastweek($week_start_num=2, strtotime('2015-03-27')); get_lastweek($week_start_num=2, strtotime('2015-03-28')); get_lastweek($week_start_num=2, strtotime('2015-03-29')); exit(); ```