开发者

PHP using todays date to get the specific days of subsequent months

Have a booking system for a travel company. It gives the planner a few options on start dates, all of which should be the first saturday of each month. So the P开发者_如何学运维HP would need to get todays date.

Then the following PHP in option box number 1 should use todays date to display the first saturday of next month. The second box show the month after that. etc.. etc..

Any help on how to do that would be marvellous.

Thanks


This example is quite verbose, but it should show the power of PHP 5.3's DateTime class and its relations.

<?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
}


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;
}

Sorry guys first time misread the question. This should work as intended but its very messy... Please optimize it guys.....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜