fanjavaid
fanjavaid

Reputation: 1738

Time calculation in PHP using Loop

i want to insert multiple data in database according to selected start_time and end_time. I already get the calculation for the time. But, when i run the query insert it weird on sixtieth minutes (in bolder output).

Here is my code :

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


And here is the output :

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

How to make 0560 to 0600 in the output above?

Thank you.

Upvotes: 0

Views: 286

Answers (5)

Wesley
Wesley

Reputation: 563

You could try something like this.

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

Upvotes: 0

Cobra_Fast
Cobra_Fast

Reputation: 16061

Solution using DateTime, requiring at least PHP version 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

Upvotes: 0

Amal Murali
Amal Murali

Reputation: 76646

You can use DateTime objects to achieve this:

$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/>";
}

Output:

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.

Upvotes: 3

Nathaniel Ford
Nathaniel Ford

Reputation: 21220

You're doing time management in a strange way; specifically as a series of string concatenations, but this is what you want:

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

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

Note that you're treating each 'digit' as it's own string, and expecting rollover from one digit to another. Handling numbers as strings is enormously error prone, and you may want to back out your code so you're never using substr.

Upvotes: 1

Leonardo
Leonardo

Reputation: 736

Instead of

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

do this:

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

Upvotes: -2

Related Questions