[英]Get the first week day in a given week number in PHP
How can I get the first week day in a given week number? 如何获得给定周数中的第一个工作日? I'm making a function in PHP for a calendar app.
我正在用PHP开发日历应用程序的功能。
The idea: When I click on a link that basically uses strtotime with +1 month it only jumps to that same day of course. 想法:当我单击一个基本上使用+1个月的strtotime的链接时,它只会跳转到课程的同一天。 I need to get the week numbers correct.
我需要得到正确的星期数。
Example: When I use the menu to move from August to September, it shouldn't select the same date I was in in August, but the first day of the first week number in September (Monday, 2th of September, week number 36). 示例:当我使用菜单从8月移到9月时,它不应该选择与8月相同的日期,而是选择9月的第一个星期的第一天(9月2日,星期一,第36周) 。
And from September to October: Week number 40, Tuesday the 1th of October. 从9月到10月:第40周,即10月1日,星期二。
I found a function that exactly like you want it. 我发现了一个完全像您想要的功能。 This is
setISODate
这是
setISODate
$date = new DateTime();
$date->setISODate(2013, 35, 1);
echo $date->format('Y-m-d');
You can change Ymd
date format as you want 您可以根据
Ymd
更改Ymd
日期格式
2013-08-26 // This week number and monday
setISODate(year, week, day)
Try this code 试试这个代码
<?php
$week = 3;
$year = 2009;
$timestamp = mktime( 0, 0, 0, 1, 1, $year ) + ( $week * 7 * 24 * 60 * 60 );
$timestamp_for_monday = $timestamp - 86400 * ( date( 'N', $timestamp ) - 1 );
$date_for_monday = date( 'Y-m-d', $timestamp_for_monday );
?>
Like every1 already said, ISO weeks start with monday. 就像每一个已经说过的那样,ISO周从星期一开始。 Quote from http://en.wikipedia.org/wiki/ISO_week_date :
Weeks start with Monday.
引用自http://en.wikipedia.org/wiki/ISO_week_date :
Weeks start with Monday.
从Weeks start with Monday.
function getFirstWeekDay($year, $week) {
$dt = (new DateTime)->setISODate($year, $week, 1);
$dtC = clone $dt;
for ($N = $dt->format('N'); $N < 7; $N++) {
$dtC->modify('+1 day');
if ($dt->format('m') !== $dtC->format('m')) {
return $dtC;
}
}
return $dt;
}
echo getFirstWeekDay(2013, 40)->format('W\t\h \w\e\e\k \i\s Y-m-d');
# 40th week is 2013-10-01
echo getFirstWeekDay(2013, 1)->format('W\t\h \w\e\e\k \i\s Y-m-d');
# 1th week is 2013-01-01
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.