计算月份中的月份,月份,年份,星期几和星期几
如何在PHP中使用给出的月份,年份,星期几和星期数来计算月份中的某天.
例如,如果我有2013年9月,并且星期几是星期五,星期数是2,我应该得到6.(2013年9月6日是第二个星期的星期五.)
How can I calculate the day of month in PHP with giving month, year, day of week and number of week.
Like, if I have September 2013 and day of week is Friday and number of week is 2, I should get 6. (9/6/2013 is Friday on the 2nd week.)
实现此目标的一种方法是使用相对格式"rel =" nofollow> strtotime()
.
One way to achieve this is using relative formats for strtotime()
.
不幸的是,它不如:
strtotime('Friday of second week of September 2013');
为使您能像您所说的那样工作几周,您需要使用相对时间戳再次调用 strtotime()
.
In order for the weeks to work as you mentioned, you need to call strtotime()
again with a relative timestamp.
$first_of_month_timestamp = strtotime('first day of September 2013');
$second_week_friday = strtotime('+1 week, Friday', $first_of_month_timestamp);
echo date('Y-m-d', $second_week_friday); // 2013-09-13
注意:由于本月的第一天从第一周开始,所以我相应地减少了一周.
Note: Since the first day of the month starts on week one, I've decremented the week accordingly.