PHP使用todays date获取后续月份的特定日期
有一个旅游公司的预订系统。它为计划者提供了一些关于开始日期的选项,所有这些都应该是每个月的第一个星期六。因此PHP需要获得今天的日期 然后,选项框1中的以下PHP应使用todays date显示下个月的第一个星期六。第二个框显示该月之后的月份。等等等 任何关于如何做到这一点的帮助都将是了不起的PHP使用todays date获取后续月份的特定日期,php,date,Php,Date,有一个旅游公司的预订系统。它为计划者提供了一些关于开始日期的选项,所有这些都应该是每个月的第一个星期六。因此PHP需要获得今天的日期 然后,选项框1中的以下PHP应使用todays date显示下个月的第一个星期六。第二个框显示该月之后的月份。等等等 任何关于如何做到这一点的帮助都将是了不起的 谢谢这个示例非常详细,但它应该显示PHP5.3的DateTime类及其关系的强大功能 <?php $first_of_next_month = new DateTime('next month f
谢谢这个示例非常详细,但它应该显示PHP5.3的DateTime类及其关系的强大功能
<?php
$first_of_next_month = new DateTime('next month first day'); // get a start date
$a_day = new DateInterval('P1D');
$a_month = new DateInterval('P1M');
$start_dates = array();
foreach (new DatePeriod($first_of_next_month, $a_month, 11) as $day_of_month) {
// $day_of_month is now the first day of the next month
while ($day_of_month->format('w') != 6) {
// while $day_of_month isn't a Saturday
$day_of_month->add($a_day);
// add a day
}
$start_dates[] = $day_of_month;
}
foreach ($start_dates as $d) {
echo $d->format('r'), "\n"; // or format how you like
}
抱歉,伙计们第一次误读了这个问题。这应该是预期的工作,但它非常混乱。。。请对it人员进行优化…date'Y-m-d',strottime'2011年2月的第一个周六';这在echoStrange上似乎没有任何作用,因为它在这里回响为“2011-02-05”。无论如何,探索strotime和它接受的格式。之前已经得到了充分的回答。请使用搜索功能,在您提交问题之前,在中向您询问。的单词导致错误,结果失败。不得在5.3上,否则无法执行下面的代码。显然它不起作用,但这里所有的解决方案都是错误的
function next_ten_first_sat(){
$next_month = strtotime(date('Y-m-d'));
$j=0;
for($i=1;$i<10;$i++){
if($i==1){
$sat_next_month[$i] = strtotime('next month first saturday', $next_month);
$next_month = strtotime(date('Y-m',$sat_next_month[$i]).'-01');
}else{
$sat_next_month[$i] = strtotime('next month first saturday', $next_month);
$next_month = strtotime(date('Y-m',$sat_next_month[$i]).'-01');
$result_year = date('Y', $sat_next_month[$i]);
if(date('m', $sat_next_month[$i])>10){
$result_month = date('m', $sat_next_month[$i])-1;
}else{
$result_month = date('m', $sat_next_month[$i])-1;
$result_month = '0'.$result_month;
}
$result_date = date('d', $sat_next_month[$i]);
$result_array[$j] = $result_year.'-'.$result_month.'-'.$result_date;
}
$j++;
}
return $result_array;
}