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

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

WBOY
WBOY原創
2016-06-07 11:39:19702瀏覽

/**
* 获取当前时间上一周的开始时间和结束时间,可以通过配置一周的开始时间,默认为星期一
* @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