我有一个小问题,我有月份(2013年12月)和周数(1,2,3等),我需要找出每周的第一个日期。
我不能做一个简单的搜索,比如找一个相对于约会的最接近的星期日,因为有些星期几天都没有开始。
任何帮助都将不胜感激。
答案 0 :(得分:2)
考虑到您拥有任何月份的第一天:
next Sunday
代码:
$firstDayOfMonth = '2013-12-01'; // Try also with first day of other months
$week1 = $firstDayOfMonth;
$week2 = date( "Y-m-d" ,strtotime('next Sunday', strtotime( $week1 ) ) );
$week3 = date( "Y-m-d" ,strtotime('+1 week', strtotime( $week2 ) ) );
$week4 = date( "Y-m-d" ,strtotime('+1 week', strtotime( $week3 ) ) );
$week5 = date( "Y-m-d" ,strtotime('+1 week', strtotime( $week4 ) ) );
echo '
Week 1 starts on : ' .$week1 .'<br>
Week 2 starts on : ' .$week2 .'<br>
Week 3 starts on : ' .$week3 .'<br>
Week 4 starts on : ' .$week4 .'<br>
Week 5 starts on : ' .$week5 .'<br>';
输出:
Week 1 starts on : 2013-12-01
Week 2 starts on : 2013-12-08
Week 3 starts on : 2013-12-15
Week 4 starts on : 2013-12-22
Week 5 starts on : 2013-12-29
答案 1 :(得分:1)
试试
<?php
$first_day_this_month = date('M-D-Y',strtotime(date('M-01-Y')));
echo date('M-D-Y',strtotime(date('M-01-Y')))." = ".date('M-d-Y',strtotime(date('M-01-Y')));//First day of month
echo "<br>";
$last_day_this_month = date('t');//last day of month
$last_day_num=intval($last_day_this_month);
for($i=1;$i<$last_day_num;$i++)
{
$first_day_this_month = date('M-D-Y',strtotime(date('M-'.$i.'-Y')));//All day of month
$dateopt_arr=date('M-N-Y',strtotime(date('M-'.$i.'-Y')));
$dateo=explode('-',$dateopt_arr);
//echo $dateo[1];
if(($dateo[1]%7)==0)
{
echo $first_day_this_month = date('M-D-Y',strtotime(date('M-'.$i.'-Y')))." = ".date('M-d-Y',strtotime(date('M-'.$i.'-Y')));//Week day of month
echo "<br>";
}
}
?>
答案 2 :(得分:0)
使用strtotime的第二个参数指定计算“下周日”的日期以及其他相关日期:
strtotime('next Sunday', strtotime('09/12/2013'));
示例:
echo date('m/d/Y', strtotime('next Sunday', strtotime('09/12/2013')));