使用Loop在PHP中进行时间计算

时间:2013-11-22 17:36:28

标签: php mysql time

我想根据选定的start_timeend_time在数据库中插入多个数据。我已经得到了时间的计算。但是,当我运行查询时,在第60分钟(更大胆的输出)插入它很奇怪。

这是我的代码:

<?php
    $start_hour = "05";
    $start_min = "30";

    $end_hour = "06";
    $end_min = "05";

    $start_hour = substr($start_hour,1,1);
    $end_hour = substr($end_hour,1,1);

    $start_time = $start_hour.$start_minute;
    $end_time = $end_hour.$end_minute;

    if($start_hour < $end_hour) {
        for($i=$start_hour; $i<=$end_hour; $i++) {
            //$i = (($i < 10))? "0".$i: $i;

            if($i == $end_hour) {
                $start_min = "00";
            }

            for($j=$start_min; $j<60; $j+=5) {
                $j = (($j < 10) && ($j != 0))? "0".$j: $j;
                $time = "0".$i.$j;
                $time2 = "0".($i.$j+5);
                echo "<br/>";   

                // Lakukan insert disini
                echo "INSERT INTO golf_list start_time='$time' AND end_time='$time2'";

                if($i == $end_hour && $j == $end_min) { break; }
            }
        }
    } 
?>


这是输出:

  

INSERT INTO golf_list start_time ='0530'AND_time ='0535'
  INSERT INTO golf_list start_time ='0535'AND_time ='0540'
  INSERT INTO golf_list start_time ='0540'AND_time ='0545'   INSERT INTO golf_list start_time ='0545'AND_time ='0550'
  INSERT INTO golf_list start_time ='0550'AND_time_time ='0555'
   INSERT INTO golf_list start_time ='0555'AND_time ='0560'
  INSERT INTO golf_list start_time ='0600'AND_time ='0605'
  INSERT INTO golf_list start_time ='0605'AND_time_time ='0610'

如何在上面的输出中将0560设为0600

谢谢。

5 个答案:

答案 0 :(得分:3)

您可以使用DateTime对象来实现此目的:

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

while ($start <= $end) {
    $start_time = $start->format('H:i');
    $start->modify('+5 minute');
    $end_time = $start->format('H:i');

    echo "INSERT INTO golf_list start_time='$start_time' AND end_time='$end_time'","<br/>";
}

输出:

INSERT INTO golf_list start_time='05:30' AND end_time='05:35'
INSERT INTO golf_list start_time='05:35' AND end_time='05:40'
INSERT INTO golf_list start_time='05:40' AND end_time='05:45'
INSERT INTO golf_list start_time='05:45' AND end_time='05:50'
INSERT INTO golf_list start_time='05:50' AND end_time='05:55'
INSERT INTO golf_list start_time='05:55' AND end_time='06:00'
INSERT INTO golf_list start_time='06:00' AND end_time='06:05'
INSERT INTO golf_list start_time='06:05' AND end_time='06:10'

Demo.

答案 1 :(得分:1)

你正在以一种奇怪的方式进行时间管理;特别是作为一系列字符串连接,但这是你想要的:

$jPrime = ($j + 5) % 60)
$jPrime = ($jPrime < 10) ? "0".$jPrime : $jPrime;
$iPrime = $i + floor(($j + 5) / 60)

$time2 = "0".($iPrime.$jPrime);

请注意,您将每个'数字'视为自己的字符串,并期望从一个数字翻转到另一个数字。将数字作为字符串处理非常容易出错,并且您可能想要退出代码,因此您永远不会使用substr

答案 2 :(得分:0)

使用DateTime的解决方案,至少需要PHP版本5.3.0:

$start = new DateTime('05:30');
$end = new DateTime('06:05');

$step = DateInterval::createFromDateString('5 minutes');

for ($i = $start; $i <= $end; $i->add($step))
    echo $i->format('Hi'); // put your query around this

答案 3 :(得分:0)

你可以尝试这样的事情。

<?php
    $start_hour = "05";
    $start_min = "30";

    $end_hour = "06";
    $end_min = "05";

    if($start_hour < $end_hour) {
        $time = $start_time = mktime($start_hour, $start_min);
        $end_time = mktime($end_hour, $end_min);
        while ($time <= $end_time) {
            $time2 = strtotime("+5 minute", $time);
            echo "INSERT INTO `golf_list` SET `start_time` = '" . date('Y-m-d H:i:s', $time) . "', `end_time` = '" . date('Y-m-d H:i:s', $time2) . "'";
            echo "\n";
            $time = $time2;
        }   
    }   
?>

答案 4 :(得分:-2)

而不是

for($j=$start_min; $j<60; $j+=5) {

这样做:

for($j=$start_min; $j<=60; $j+=5) {