在第三个星期六的日期提取期间出错

时间:2015-11-21 10:17:24

标签: php arrays date

我想获取第三个星期六,我正在使用PHP功能,我知道。 但是从错误中获取时我收到的数据错误。 这是我的代码:

@ManagedBean(name = "aaaBean")
@RequestScoped
public class CustomerBean{
-----
}

我哪里错了?

3 个答案:

答案 0 :(得分:6)

每个月只包含一个"第三个星期六" ,所以不需要做更多循环的日子。试试这段代码吧。

$frmdate = "2015-06-05";
$todate = "2015-08-31";
$custom_third_sat=array();
for ($date = date("Y-m-01", strtotime($frmdate)); $date <= $todate; $date = date("Y-m-01",strtotime($date."+1 Month"))) {
    if($date>$todate){
        break;
    }
    $t_date=date('Y-m-d', strtotime($date.' third Saturday'));
    if($t_date>=$frmdate && $t_date<=$todate)
    {
        $custom_third_sat[] = $t_date;
    }

}
echo "<pre>";print_r($custom_third_sat);

答案 1 :(得分:1)

你应该像of一样使用third saturday of:试试这个

$custom_third_sat[] = date('Y-m-d', strtotime("third saturday of $custom_day"));

您的完整代码可能是这样的:

$frmdate = '2015-06-05';
$todate = '2015-08-31';
for ($date = strtotime($frmdate); $date <= strtotime($todate); $date = strtotime("+1 day", $date)) 
        {
            $custom_day = date("Y-m-d", $date);
if(!isset($custom_third_sat[date('Y-m-d', strtotime("third saturday of $custom_day"))])){
        $custom_third_sat[date('Y-m-d', strtotime("third saturday of $custom_day"))] = date('Y-m-d', strtotime("third saturday of $custom_day"));
}

        }
        echo "<pre>";
        print_r($custom_third_sat);

答案 2 :(得分:0)

您只是错过了日期的引号

<?php
$frmdate = '2015-06-05';
$todate = '2015-08-31';

for ($date = strtotime($frmdate); $date <= strtotime($todate); $date = strtotime("+1 day", $date)) 
    {
    echo"assa";
        $custom_day = date("Y-m-d", $date);

        $custom_third_sat[] = date('Y-m-d', strtotime('third Saturday "'.$custom_day.'"'));

    }
    echo "<pre>";
    print_r($custom_third_sat);

&GT;