在PHP中获得第一个星期的一天?

给定一个MM-dd-yyyy格式的date,有人能帮我得到一周的第一天吗?

这是我正在使用的…

 $day = date('w'); $week_start = date('md-Y', strtotime('-'.$day.' days')); $week_end = date('md-Y', strtotime('+'.(6-$day).' days')); 

$ day包含从0到6的数字,表示星期几(星期日= 0,星期一= 1等)。
$ week_start包含当前星期日的date,为mm-dd-yyyy。
$ week_end包含当前星期六的date为mm-dd-yyyy。

 strtotime('this week', time()); 

replace时间()。 如果当天是星期日/星期一,则下一个星期日/星期一的方法将不起作用。

使用strtotime函数非常简单:

 echo date("Ymd", strtotime('monday this week')), "\n"; echo date("Ymd", strtotime('sunday this week')), "\n"; 

它在PHP版本中有点不同 :

5.3.0 – 5.6.6,php7 @ 20140507 – 20150301,hhvm-3.3.1 – 3.5.1的输出

 2015-03-16 2015-03-22 

输出为4.3.5 – 5.2.17

 2015-03-23 2015-03-22 

输出为4.3.0 – 4.3.4

 2015-03-30 2015-03-29 

比较边缘情况

本周的相关描述都有自己的上下文。 以下是星期一和星期天的星期一或星期天的输出:

 $date = '2015-03-16'; // monday echo date("Ymd", strtotime('monday this week', strtotime($date))), "\n"; echo date("Ymd", strtotime('sunday this week', strtotime($date))), "\n"; $date = '2015-03-22'; // sunday echo date("Ymd", strtotime('monday this week', strtotime($date))), "\n"; echo date("Ymd", strtotime('sunday this week', strtotime($date))), "\n"; 

在PHP版本中,它有点不同 :

5.3.0 – 5.6.6,php7 @ 20140507 – 20150301,hhvm-3.3.1 – 3.5.1的输出

 2015-03-16 2015-03-22 2015-03-23 2015-03-29 

输出为4.3.5 – 5.0.5,5.2.0 – 5.2.17

 2015-03-16 2015-03-22 2015-03-23 2015-03-22 

输出为5.1.0 – 5.1.6

 2015-03-23 2015-03-22 2015-03-23 2015-03-29 

输出为4.3.0 – 4.3.4

 2015-03-23 2015-03-29 2015-03-30 2015-03-29 

把事情简单化 :

 <?php $dateTime = new \DateTime('2012-05-14'); $monday = clone $dateTime->modify(('Sunday' == $dateTime->format('l')) ? 'Monday last week' : 'Monday this week'); $sunday = clone $dateTime->modify('Sunday this week'); ?> 

来源: PHP手册

 $givenday = date("w", mktime(0, 0, 0, MM, dd, yyyy)); 

这给出了给定date本周的星期几,其中0 =星期日, 6 =星期六。 从那里你可以简单地计算出你想要的日子。

在确定一个一致的参考框架时,以下是时光stream行的细节:

http://gamereplays.org/reference/strtotime.php

基本上只有这些string会可靠地给你相同的date,不pipe你打电话给他们一周中的哪一天。

 strtotime("next monday"); strtotime("this sunday"); strtotime("last sunday"); 

下面的代码应该适用于任何自定义的date,只是使用所需的date格式。

 $custom_date = strtotime( date('dm-Y', strtotime('31-07-2012')) ); $week_start = date('dm-Y', strtotime('this week last monday', $custom_date)); $week_end = date('dm-Y', strtotime('this week next sunday', $custom_date)); echo '<br>Start: '. $week_start; echo '<br>End: '. $week_end; 

我用PHP 5.2.17testing了代码结果:

 Start: 30-07-2012 End: 05-08-2012 

这个问题需要一个很好的DateTime答案:

 function firstDayOfWeek($date) { $day = DateTime::createFromFormat('md-Y', $date); $day->setISODate((int)$day->format('o'), (int)$day->format('W'), 1); return $day->format('md-Y'); } var_dump(firstDayOfWeek('06-13-2013')); 

输出: –

 string '06-10-2013' (length=10) 

这将处理年份和闰年。

这个怎么样?

 $first_day_of_week = date('md-Y', strtotime('Last Monday', time())); $last_day_of_week = date('md-Y', strtotime('Next Sunday', time())); 

在这里,我正在考虑将星期天定为一周和周六的最后一天。

 $m = strtotime('06-08-2012'); $today = date('l', $m); $custom_date = strtotime( date('dm-Y', $m) ); if ($today == 'Sunday') { $week_start = date("dmY", $m); } else { $week_start = date('dm-Y', strtotime('this week last sunday', $custom_date)); } if ($today == 'Saturday') { $week_end = date("dmY", $m); } else { $week_end = date('dm-Y', strtotime('this week next saturday', $custom_date)); } echo '<br>Start: '. $week_start; echo '<br>End: '. $week_end; 

输出:

开始时间:05-08-2012
完:2012年8月11日

这个怎么样?

 $day_of_week = date('N', strtotime($string_date)); $week_first_day = date('Ym-d', strtotime($string_date . " - " . ($day_of_week - 1) . " days")); $week_last_day = date('Ym-d', strtotime($string_date . " + " . (7 - $day_of_week) . " days")); 

这是我用来从任何date获得一周的第一天和最后一天。 在这种情况下,星期一是本周的第一天…

 $date = date('Ym-d') // you can put any date you want $nbDay = date('N', strtotime($date)); $monday = new DateTime($date); $sunday = new DateTime($date); $monday->modify('-'.($nbDay-1).' days'); $sunday->modify('+'.(7-$nbDay).' days'); 

假设星期一是一周的第一天,这是有效的:

 echo date("Mdy", strtotime('last monday', strtotime('next week', time()))); 

尝试这个:

 function week_start_date($wk_num, $yr, $first = 1, $format = 'F d, Y') { $wk_ts = strtotime('+' . $wk_num . ' weeks', strtotime($yr . '0101')); $mon_ts = strtotime('-' . date('w', $wk_ts) + $first . ' days', $wk_ts); return date($format, $mon_ts); } $sStartDate = week_start_date($week_number, $year); $sEndDate = date('F d, Y', strtotime('+6 days', strtotime($sStartDate))); 

(从这个论坛主题 )

这是我发现的最短和最可读的解决scheme:

  <?php $weekstart = strtotime('monday this week'); $weekstop = strtotime('sunday this week 23:59:59'); //echo date('dmY H:i:s', $weekstart) .' - '. date('dmY H:i:s', $weekstop); ?> 

strtotimenew DateTime()->getTimestamp()更快。

鉴于PHP版本5.3之前的函数给出了给定date的星期几(在这种情况下 – 星期日,2013-02-03):

 <?php function startOfWeek($aDate){ $d=strtotime($aDate); return strtotime(date('Ym-d',$d).' - '.date("w",$d).' days'); } echo(date('Ym-d',startOfWeek("2013-02-07")).' '); ?> 
 $today_day = date('D'); //Or add your own date $start_of_week = date('Ymd'); $end_of_week = date('Ymd'); if($today_day != "Mon") $start_of_week = date('Ymd', strtotime("last monday")); if($today_day != "Sun") $end_of_week = date('Ymd', strtotime("next sunday")); 
 $monday = date('dm-Y',strtotime('last monday',strtotime('next monday',strtotime($date)))); 

你必须先得到下一个星期一,然后得到下一个星期一的“最后一个星期一”。 所以如果给定的date是星期一,它将会在星期一返回相同的date。

如果你想星期一作为你的一周的开始,做到这一点:

 $date = '2015-10-12'; $day = date('N', strtotime($date)); $week_start = date('Ym-d', strtotime('-'.($day-1).' days', strtotime($date))); $week_end = date('Ym-d', strtotime('+'.(7-$day).' days', strtotime($date))); 

你使用strptime()parsingdate,并在结果上使用date()

 date('N', strptime('%m-%d-%g', $dateString)); 
 <?php /* PHP 5.3.0 */ date_default_timezone_set('America/Denver'); //Set apprpriate timezone $start_date = strtotime('2009-12-15'); //Set start date //Today's date if $start_date is a Sunday, otherwise date of previous Sunday $today_or_previous_sunday = mktime(0, 0, 0, date('m', $start_date), date('d', $start_date), date('Y', $start_date)) - ((date("w", $start_date) ==0) ? 0 : (86400 * date("w", $start_date))); //prints 12-13-2009 (month-day-year) echo date('md-Y', $today_or_previous_sunday); ?> 

(注:在问题中的MM,DD和YYYY是不是标准的PHPdate格式语法 – 我不能确定是什么意思,所以我设置ISO开始date与ISO年年日)

另一种方法来做到这一点….

 $year = '2014'; $month = '02'; $day = '26'; $date = DateTime::createFromFormat('Ymd H:i:s', $year . '-' . $month . '-' . $day . '00:00:00'); $day = date('w', $date->getTimestamp()); // 0=Sunday 6=Saturday if($day!=0){ $newdate = $date->getTimestamp() - $day * 86400; //86400 seconds in a day // Look for DST change if($old = date('I', $date->getTimestamp()) != $new = date('I', $newdate)){ if($old == 0){ $newdate -= 3600; //3600 seconds in an hour } else { $newdate += 3600; } } $date->setTimestamp($newdate); } echo $date->format('D Ymd H:i:s'); 

获得本周第一天(星期一)的最简单方法是:

 strtotime("next Monday") - 604800; 

其中604800 – 是一周内的秒数(60 * 60 * 24 * 7)。

这个代码在下个星期一得到,并减less一个星期。 这个代码在一周中的任何一天都可以正常工作。 即使今天是星期一。

考虑到我的时区是澳大利亚,而且strtotime()不喜欢英国的date,我觉得这很令人沮丧。

如果当天是星期天,则strtotime("monday this week")将在第二天返回。

为了克服这一点:

注意 :这只适用于澳大利亚/英国的date

 $startOfWeek = (date('l') == 'Monday') ? date('d/m/Y 00:00') : date('d/m/Y', strtotime("last monday 00:00")); $endOfWeek = (date('l') == 'Sunday') ? date('d/m/Y 23:59:59') : date('d/m/Y', strtotime("sunday 23:59:59")); 

这样做的一个聪明的方法是让PHP处理时区差异和夏令时(DST)。 让我告诉你如何做到这一点。

此function将从星期一至星期五(包括周一至周五)生成所有date(方便生成工作周日):

 class DateTimeUtilities { public static function getPeriodFromMondayUntilFriday($offset = 'now') { $now = new \DateTimeImmutable($offset, new \DateTimeZone('UTC')); $today = $now->setTime(0, 0, 1); $daysFromMonday = $today->format('N') - 1; $monday = $today->sub(new \DateInterval(sprintf('P%dD', $daysFromMonday))); $saturday = $monday->add(new \DateInterval('P5D')); return new \DatePeriod($monday, new \DateInterval('P1D'), $saturday); } } foreach (DateTimeUtilities::getPeriodFromMondayUntilFriday() as $day) { print $day->format('c'); print PHP_EOL; } 

这将返回本周的周一到周五的date时间。 要为任意date执行相同操作,请将date作为parameter passing给DateTimeUtilities ::getPeriodFromMondayUntilFriday ,从而:

 foreach (DateTimeUtilities::getPeriodFromMondayUntilFriday('2017-01-02T15:05:21+00:00') as $day) { print $day->format('c'); print PHP_EOL; } //prints //2017-01-02T00:00:01+00:00 //2017-01-03T00:00:01+00:00 //2017-01-04T00:00:01+00:00 //2017-01-05T00:00:01+00:00 //2017-01-06T00:00:01+00:00 

星期一只对OP有兴趣?

 $monday = DateTimeUtilities::getPeriodFromMondayUntilFriday('2017-01-02T15:05:21+00:00')->getStartDate()->format('c'); print $monday; // prints //2017-01-02T00:00:01+00:00 

我几次反对这个问题,并总是惊讶的datefunction不使这更容易或更清晰。 这是我使用DateTime类的PHP5解决scheme:

 /** * @param DateTime $date A given date * @param int $firstDay 0-6, Sun-Sat respectively * @return DateTime */ function getFirstDayOfWeek(DateTime $date, $firstDay = 0) { $offset = 7 - $firstDay; $ret = clone $date; $ret->modify(-(($date->format('w') + $offset) % 7) . 'days'); return $ret; } 

必须克隆以避免更改原始date。

关于什么:

 $date = "2013-03-18"; $searchRow = date("dmY", strtotime('last monday',strtotime($date." + 1 day"))); echo "for date " .$date ." we get this monday: " ;echo $searchRow; echo '<br>'; 

它不是最好的方式,但我testing,如果我在这个星期我得到正确的星期一,如果我在星期一我会得到那个星期一。

我发现这个解决scheme很有用 只要不是星期一就可以得到前一个星期一。 我使用$ lower_date作为从查询中提取的date,然后我需要和前一个星期一进行协调。

 //set this up to go backwards until you find monday while(date('D',strtotime($lower_date))!='Mon'){ $lower_date = date('Ym-d', strtotime($lower_date . ' - 1 day')); //increase the upper spec } 

这一个是今天准备的是星期一

 function lastMonday(\DateTime $date) { $timestamp = $date->getTimestamp(); $monday = ( 1 == date( 'N', $timestamp )); if ($monday) { return $timestamp; } else { return strtotime( 'last monday', $timestamp ); } } 

如果你想要得到时间戳,而不是DateTime更改前两行(摆脱date-> getTimestamp)改变他们只是这个

 function lastMonday($timestamp) { 

如果你想要它inputstring改变这两行

 function lastMonday($dateString) { $timestamp = strtotime($dateString); 

我正在寻找一个类似于这个的解决scheme,我终于想出了一些将在本周每天返回的东西。

 //set current timestamp $today = time(); //calculate the number of days since Monday $dow = date('w', $today); $offset = $dow - 1; if ($offset < 0) { $offset = 6; } //calculate timestamp for Monday and Sunday $monday = $today - ($offset * 86400); $tuesday = $monday + (1 * 86400); $wednesday = $monday + (2 * 86400); $thursday = $monday + (3 * 86400); $friday = $monday + (4 * 86400); $saturday = $monday + (5 * 86400); $sunday = $monday + (6 * 86400); //print dates for Monday and Sunday in the current week print date("Ymd", $monday) . "\n"; print date("Ymd", $tuesday) . "\n"; print date("Ymd", $wednesday) . "\n"; print date("Ymd", $thursday) . "\n"; print date("Ymd", $friday) . "\n"; print date("Ymd", $saturday) . "\n"; print date("Ymd", $sunday) . "\n"; 

感谢你在这里发布这个的dbunic: http : //www.redips.net/php/week-list-current-date/#comments