$d = strtotime(“today”);
$start_week = strtotime(“last sunday midnight”,$d);
$end_week = strtotime(“next saturday”,$d);
$start = date(“Y-m-d”,$start_week);
$end = date(“Y-m-d”,$end_week);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
/** * 功能:取得给定日期所在周的開始日期和結束日期 * 参數:$gdate 日期,默認为當天,格式:YYYY-MM-DD * $first 一周以星期一還是星期天開始,0为星期天,1为星期一 * 返回:數組array("開始日期", "結束日期"); * */ function aweek($gdate = "", $first = 0){ if(!$gdate) $gdate = date("Y-m-d"); $w = date("w", strtotime($gdate));//取得一周的第幾天,星期天開始0-6 $dn = $w ? $w - $first : 6;//要減去的天數 //本周開始日期 $st = date("Y-m-d", strtotime("$gdate -".$dn." days")); //本周結束日期 $en = date("Y-m-d", strtotime("$st +6 days")); //上周開始日期 $last_st = date('Y-m-d',strtotime("$st - 7 days")); //上周結束日期 $last_en = date('Y-m-d',strtotime("$st - 1 days")); return array($st, $en,$last_st,$last_en);//返回開始和結束日期 } echo implode("|", aweek("", 1)).'<br />'; //echo date("Y-m-d",strtotime("time()")); echo '本周第一天(星期日为一周開始):'.date('Y-m-d', time()-86400*date('w')).'<br/>'; echo '本周第一天(星期一为一周開始):'.date('Y-m-d', time()-86400*date('w')+(date('w')>0?86400:-6*86400)).'<br/>'; echo '本月第一天:'.date('Y-m-d', mktime(0,0,0,date('m'),1,date('Y'))).'<br/>'; echo '本月最後一天:'.date('Y-m-d', mktime(0,0,0,date('m'),date('t'),date('Y'))).'<br/>'; //上個月的開始日期 $m = date('Y-m-d', mktime(0,0,0,date('m')-1,1,date('Y'))); //上個月共多少天 $t = date('t',strtotime("$m")); echo '上月第一天:'.date('Y-m-d', mktime(0,0,0,date('m')-1,1,date('Y'))).'<br/>'; echo '上月最後一天:'.date('Y-m-d', mktime(0,0,0,date('m')-1,$t,date('Y'))).'<br/>'; ?> |
PHP手冊上有一個這個方法,用來返回指定日期的周一和周日
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php function get_week_range($week, $year) { $timestamp = mktime(1, 0, 0, 1, 1, $year); $firstday = date("N", $timestamp); if ($firstday > 4) $firstweek = strtotime('+' . (8 - $firstday) . ' days', $timestamp); else $firstweek = strtotime('-' . ($firstday - 1) . ' days', $timestamp); $monday = strtotime('+' . ($week - 1) . ' week', $firstweek); $sunday = strtotime('+6 days', $monday); $start = date("Y-m-d", $monday); $end = date("Y-m-d", $sunday); return array( $start, $end ); } ?> |
strtotime獲取本周第一天和最後一天方法的BUG PHP手冊上有一個這個方法,用來返回指定日期的周一和周日
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php function get_week_range($week, $year) { $timestamp = mktime(1, 0, 0, 1, 1, $year); $firstday = date("N", $timestamp); if ($firstday > 4) $firstweek = strtotime('+' . (8 - $firstday) . ' days', $timestamp); else $firstweek = strtotime('-' . ($firstday - 1) . ' days', $timestamp); $monday = strtotime('+' . ($week - 1) . ' week', $firstweek); $sunday = strtotime('+6 days', $monday); $start = date("Y-m-d", $monday); $end = date("Y-m-d", $sunday); return array( $start, $end ); } ?> |
但在跨年的時候使用會有問題例如2009年的12月31日週四和2010年1月1日週五週拿到的周一和周日完全不同2009年12月31日拿合到的周一和周日分別對應2009-12-28 2010-01-03 但2010年1月1日拿到的周一和周日分別對應2011-01-03 2011-01-09 原因為傳進去的方法的周為第53週,但是年為2010年,所以認為2010的第53週,所以計算有誤,解決方法為,如果週為大於10(因為一月個月不可能有10週),且月份為1的時候,將年減1處理
1 2 3 |
if(date('m',$last_week_time) == '01' and $tmp_last_week > 10) { $last_week_year--; } |