欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

获取当前时间上一周的开始时间和结束时间

程序员文章站 2022-06-13 20:07:04
...
/**
* 获取当前时间上一周的开始时间和结束时间,可以通过配置一周的开始时间,默认为星期一
* @param
* $begin 上一周开始时间的变量名称;
* $end 上一周结束时间的变量名称;
* $week_start_num 一周的开始时间,默认为周一
* $now_time 当前时间,这里做一个当前时间方便测试该方法的正确性
* @return array()
* @author leeyi

/**
* 获取当前时间上一周的开始时间和结束时间,可以通过配置一周的开始时间,默认为星期一
* @param
* $begin 上一周开始时间的变量名称;
* $end 上一周结束时间的变量名称;
* $week_start_num 一周的开始时间,默认为周一
* $now_time 当前时间,这里做一个当前时间方便测试该方法的正确性
* @return array()
* @author leeyi
* @spec
* 使用方法,执行
* extract(get_lastweek('beginLastweek','endLastweek'));
* 之后,可以直接使用 $beginLastweek $endLastweek 两个变量;
* 如果需要其他变量名称,可以修改参数 $begin 和 $end ,自行指定名称
* @end
*/
function get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=1, $now_time='') {
$now_time = $now_time>0 ? $now_time : time();
$now_weekday = date('w', $now_time); // 获取当前是星期前 0-6 星期日-星期六
$week_start_num = in_array($week_start_num, array('0','1','2','3','4','5','6')) ? $week_start_num : 1; // 默认一周开始时间为周一
$now_weekday = $now_weekday //php获取上周起始时间戳和结束时间戳
$beginLastweek = $now_time-($now_weekday+7-$week_start_num)*86400;
$endLastweek = $beginLastweek+(6*86400);
echo date('Y-m-d',$now_time).' : '.date('Y-m-d 00:00:00', $beginLastweek).' => '.date('Y-m-d 23:59:59', $endLastweek).'
'; // for test
return array(
$begin => strtotime(date('Y-m-d 00:00:00', $beginLastweek)),
$end => strtotime(date('Y-m-d 23:59:59', $endLastweek))
);
}
下面是测试代码 :get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=0, strtotime('2015-03-22'));
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=0, strtotime('2015-03-23'));
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=0, strtotime('2015-03-24'));
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=0, strtotime('2015-03-25'));
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=0, strtotime('2015-03-26'));
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=0, strtotime('2015-03-27'));
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=0, strtotime('2015-03-28'));
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=0, strtotime('2015-03-29'));
echo '
';
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=1, strtotime('2015-03-22'));
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=1, strtotime('2015-03-23'));
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=1, strtotime('2015-03-24'));
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=1, strtotime('2015-03-25'));
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=1, strtotime('2015-03-26'));
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=1, strtotime('2015-03-27'));
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=1, strtotime('2015-03-28'));
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=1, strtotime('2015-03-29'));
echo '
';
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=2, strtotime('2015-03-22'));
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=2, strtotime('2015-03-23'));
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=2, strtotime('2015-03-24'));
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=2, strtotime('2015-03-25'));
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=2, strtotime('2015-03-26'));
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=2, strtotime('2015-03-27'));
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=2, strtotime('2015-03-28'));
get_lastweek($begin='beginLastweek',$end='endLastweek',$week_start_num=2, strtotime('2015-03-29'));
exit();
浏览器输出结果:

2015-03-22 : 2015-03-15 00:00:00 => 2015-03-21 23:59:59
2015-03-23 : 2015-03-15 00:00:00 => 2015-03-21 23:59:59
2015-03-24 : 2015-03-15 00:00:00 => 2015-03-21 23:59:59
2015-03-25 : 2015-03-15 00:00:00 => 2015-03-21 23:59:59
2015-03-26 : 2015-03-15 00:00:00 => 2015-03-21 23:59:59
2015-03-27 : 2015-03-15 00:00:00 => 2015-03-21 23:59:59
2015-03-28 : 2015-03-15 00:00:00 => 2015-03-21 23:59:59
2015-03-29 : 2015-03-22 00:00:00 => 2015-03-28 23:59:59

2015-03-22 : 2015-03-09 00:00:00 => 2015-03-15 23:59:59
2015-03-23 : 2015-03-16 00:00:00 => 2015-03-22 23:59:59
2015-03-24 : 2015-03-16 00:00:00 => 2015-03-22 23:59:59
2015-03-25 : 2015-03-16 00:00:00 => 2015-03-22 23:59:59
2015-03-26 : 2015-03-16 00:00:00 => 2015-03-22 23:59:59
2015-03-27 : 2015-03-16 00:00:00 => 2015-03-22 23:59:59
2015-03-28 : 2015-03-16 00:00:00 => 2015-03-22 23:59:59
2015-03-29 : 2015-03-16 00:00:00 => 2015-03-22 23:59:59

2015-03-22 : 2015-03-10 00:00:00 => 2015-03-16 23:59:59
2015-03-23 : 2015-03-10 00:00:00 => 2015-03-16 23:59:59
2015-03-24 : 2015-03-17 00:00:00 => 2015-03-23 23:59:59
2015-03-25 : 2015-03-17 00:00:00 => 2015-03-23 23:59:59
2015-03-26 : 2015-03-17 00:00:00 => 2015-03-23 23:59:59
2015-03-27 : 2015-03-17 00:00:00 => 2015-03-23 23:59:59
2015-03-28 : 2015-03-17 00:00:00 => 2015-03-23 23:59:59
2015-03-29 : 2015-03-17 00:00:00 => 2015-03-23 23:59:59

AD:真正免费,域名+虚机+企业邮箱=0元