>php教程 >php手册 >获取当前时间上一周的开始时间和结束时间

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

WBOY
WBOY원래의
2016-06-07 11:39:19703검색

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

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

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.