0

I have a query to get an array from a database.

$result = mysqli_query($con,"SELECT `Time_D` from `Schedule` where `Stop` = 1 ");

while ($row = mysqli_fetch_array($result) ) {                         
echo $row['Time_D'] . "<br>";
}

This array ($row) contains a lot of times, if I'm not mistaken.

I now want to add for example 10 minutes to all the elements in the array. How do I do that?

3
  • What type is 'Time_D' ? Timestamp, int, ...? Commented Oct 17, 2014 at 9:54
  • Why every answer is about mysql UPDATE? I dont see that in the question? He wants to add to all the elements in the array Commented Oct 17, 2014 at 10:01
  • Wow, that's a lot of response! 'Time_D' type = time. And it is true I want to put the array back in a database, even though it was not in the question :). I should have asked that instead, my bad. I do not want to just update the values though, I want them in a new table/column/added below in the same column so I can show them via a new query. Commented Oct 17, 2014 at 11:23

5 Answers 5

1

Well, figured it out myself after all.

$result = mysqli_query($con,"SELECT `Time_D` from `Schedule` where `Stop` = 1 ");

//get database result
while ($row = mysqli_fetch_array($result) ) {                         
$rows[] = $row['Time_D']; //store all the times in one array
}
print_r ($rows);

echo "<br>";
$time_difference=600;
$i=0;

while (($i) < (count($rows)) ) {
$rows[$i] = ( strtotime($rows[$i]) ) + ($time_difference); //with timestamps, because, well, php and times...
$rows[$i] = date('H:i:s', ($rows[$i]) ); // back from timestamp to time
$i = $i +1;
}
print_r($rows);
Sign up to request clarification or add additional context in comments.

Comments

0

You need to update like this

$row['Time_D'] = time() + 600;

or just 10 seconds

$row['Time_D'] += 600;

Then push it back to the database

mysql_query('update table_name set Time_D = '{$row['Time_D']}' where id = '{$row['id']}'

OR something to that end.

Comments

0

You can do it in SQL only:

"UPDATE `Schedule` SET `Time_D` VALUES = DATE_ADD(`Schedule`.`Time_D`,INTERVAL 10 MINUTE) where `Stop` = 1"

1 Comment

That seems to be an update query, but I don't want to update the values, I want to add 10 minutes to the existing times and make a new array and a new set of values in the database below or aside the existing values.
0

You might be mistaken. $row does not contain a lot of times. As long as there are still records available, $row is assigned the next row of the query result as an array. If you want to alter all the times, save them to a new array. In your while-loop (don't forget to declare $rows):

$rows[] = $row['Time_D'];

$rows now stores all the times. You can then (after the while-loop) iterate over $rows and alter the data.

$rows = array_map(function($time) {
    return $time + (60 * 10); // Given $time is a timestamp
}, $rows);

If you don't want to save the times (i.e. directly output them in the while-loop), save the overhead of array_map and do it directly in the while-loop:

echo $row['Time_D'] + 60*10;

Depending on your exact situation you might want to use a key-value (i.e. id-to-time) storage for the times - otherwise you won't be able to refer them back to the "schedule" you are using.

If you only want to update the database records see Axel's answer.

4 Comments

You're absolutely right there in your first paragraph! Thanks a lot for that! I used print_r($rows); to debug, and it now indeed shows all the times in the array. Now, when I print $rows after your function, I get as result "Array ( [0] => 608 [1] => 609 [2] => 610 [3] => 611" . Maybe it has something to do with timestamps? You come up in your function with $time, and then say given $time is a timestamp. Could you elaborate on that?
To be clear: I want to save the times in an array that I can insert in a table (I think that's possible, right). I don't want to update the database records, but insert the new records seperately.
Can you give me an example of a row from the query result? What's the format of Time_D?
The type of Time_D in the database is time (not timestamp). You mean an example of the working query of the original records? The output of print_r($rows) is "Array ( [0] => 08:00:00 [1] => 09:00:00 [2] => 10:00:00"
0

You can do it from the mysql query itself by using DATE_ADD.

"SELECT DATE_ADD(`Time_D`,INTERVAL 10 MINUTE) AS 'Time_D' FROM `Schedule` WHERE `Stop` = 1 " 

You can have a parameter like this in your php script:

$minutes_to_add = 10;

$result = mysqli_query($con,"SELECT DATE_ADD(`Time_D`,INTERVAL ".$minutes_to_add." MINUTE) AS 'Time_D' FROM `Schedule` WHERE `Stop` = 1 ");

while ($row = mysqli_fetch_array($result) )
{                         
    echo $row['Time_D'] . "<br>";
}

4 Comments

I must say the code looks beautifully simple, but unfortunately when I put it in my php file it doesn't output anything. when I try the query directly in phpmyadmin, it outputs the column 'Time_D' but as values only 'null' instead of the times. What am I doing wrong?
I just noticed an error in phpmyadmin: "Current selection does not contain a unique column. Grid edit, checkbox, Edit, Copy and Delete features are not available."
@PHPirate im guessing that the 'Time_D' is not of type "DATETIME". Maybe it is just type "TEXT" ? If it is not "DATETIME" then you must parse the whatever text you get, make the appropriate transormation to time ( strtotime php.net/manual/en/function.strtotime.php may help) and then add 10 minutes
I figured it out already. And 'Time_D' is not of type "DATETIME", but of "TIME". I don't think there's much wrong with that? I'd rather have stored it as timestamp, but this way it's more readable so I can be sure what's in my database. And yes, strtotime did help actually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.