在所选日期期间循环日期和时间

时间:2013-11-28 03:59:09

标签: php datetime for-loop

晕,我在PHP中循环日期和时间有问题。我有开始日期,结束日期,开始时间和结束时间的初始值。 我希望将20131201循环到20131210,时间从05400600

我运行我的代码,但它只是在20131201循环。

INSERT INTO golf_list VALUES ('', 'others', '', '20131201', '20131210', '0540', '0545', 'IDR', '4','', 'royal', '0')
INSERT INTO golf_list VALUES ('', 'others', '', '20131201', '20131210', '0545', '0550', 'IDR', '4','', 'royal', '0')
INSERT INTO golf_list VALUES ('', 'others', '', '20131201', '20131210', '0550', '0555', 'IDR', '4','', 'royal', '0')
INSERT INTO golf_list VALUES ('', 'others', '', '20131201', '20131210', '0555', '0600', 'IDR', '4','', 'royal', '0')

我希望它循环并得到如下结果:

INSERT INTO golf_list VALUES ('', 'others', '', '20131201', '20131210', '0540', '0545', 'IDR', '4','', 'royal', '0')
INSERT INTO golf_list VALUES ('', 'others', '', '20131201', '20131210', '0545', '0550', 'IDR', '4','', 'royal', '0')
INSERT INTO golf_list VALUES ('', 'others', '', '20131201', '20131210', '0550', '0555', 'IDR', '4','', 'royal', '0')
INSERT INTO golf_list VALUES ('', 'others', '', '20131201', '20131210', '0555', '0600', 'IDR', '4','', 'royal', '0')

INSERT INTO golf_list VALUES ('', 'others', '', '20131202', '20131210', '0540', '0545', 'IDR', '4','', 'royal', '0')
INSERT INTO golf_list VALUES ('', 'others', '', '20131202', '20131210', '0545', '0550', 'IDR', '4','', 'royal', '0')
INSERT INTO golf_list VALUES ('', 'others', '', '20131202', '20131210', '0550', '0555', 'IDR', '4','', 'royal', '0')
INSERT INTO golf_list VALUES ('', 'others', '', '20131202', '20131210', '0555', '0600', 'IDR', '4','', 'royal', '0')

INSERT INTO golf_list VALUES ('', 'others', '', '20131203', '20131210', '0540', '0545', 'IDR', '4','', 'royal', '0')
INSERT INTO golf_list VALUES ('', 'others', '', '20131203', '20131210', '0545', '0550', 'IDR', '4','', 'royal', '0')
INSERT INTO golf_list VALUES ('', 'others', '', '20131203', '20131210', '0550', '0555', 'IDR', '4','', 'royal', '0')
INSERT INTO golf_list VALUES ('', 'others', '', '20131203', '20131210', '0555', '0600', 'IDR', '4','', 'royal', '0')

......等等......直到10步......

这是我的代码:

$start_date = "20131201";
    $end_date = "20131210";

    $start_hour = "05";
    $start_min = "40";

    $end_hour = "06";
    $end_min = "00";
    //$end_time = $end_hour."".$end_minute;

    //$players = $_POST['players'];
    /*
    $price = $_POST['price'];
    $price_standard = $_POST['price_standard'];
    $price_extra = $_POST['price_extra'];
    */
    $price_type = 4;
$details = "";

    $start = new DateTime("$start_hour$start_min");
    $end = new DateTime("$end_hour$end_min");

    for($i = $start_date; $i <= $end_date; $i++) {
        while($start < $end) {
            $start_time = $start->format('Hi');
            $start->modify('+5 minute');
            $end_time = $start->format('Hi');

            $query1 = "INSERT INTO golf_list (golf_name, golf_type,  location, start_date, end_date, start_time, 
                        end_time, currency, price_type, details, gate, status)
                        VALUES ('', 'others', '', '$i', '$end_date', '$start_time', 
                                '$end_time', 'IDR', '$price_type','$details', 'royal', '0')";

            echo "$query1<br/>";
        }
    }

任何解决方案?非常感谢你

1 个答案:

答案 0 :(得分:2)

$start_date = "20131201";
    $end_date = "20131210";

    $start_hour = "05";
    $start_min = "40";

    $end_hour = "06";
    $end_min = "00";
    //$end_time = $end_hour."".$end_minute;

    //$players = $_POST['players'];
    /*
    $price = $_POST['price'];
    $price_standard = $_POST['price_standard'];
    $price_extra = $_POST['price_extra'];
    */
    $price_type = 4;
    $details = "";



    for($i = $start_date; $i <= $end_date; $i++) {

    $start = new DateTime("$start_hour$start_min");
    $end = new DateTime("$end_hour$end_min");


        for($j = $start; $j < $end; $j++) {
            $start_time = $start->format('Hi');
            $start->modify('+5 minute');
            $end_time = $start->format('Hi');


            $query1 = "INSERT INTO golf_list (golf_name, golf_type,  location, start_date, end_date, start_time, 
                        end_time, currency, price_type, details, gate, status)
                        VALUES ('', 'others', '', '$i', '$end_date', '$start_time', 
                                '$end_time', 'IDR', '$price_type','$details', 'royal', '0')";

            echo "$query1<br/>";
        }
    }