Hi; I find manipulating time and date stuff between MySQL and php to be very confusing.

Say I've got time of day values in a MySQL database (TIME) field that returns me values like this:

07:30:00
15:30:00
12:00:00

So I pull these values with a query and want to add a number of minutes to them and store the result in another time field for a different purpose

Say
$time="15:30:00";
$mins=45;

How do I add $mins to time so it winds up correctly saying:

$time_plus_mins="16:15:00";

(all that str2time, mktime etc. stuff tends to get me in a real muddle for some reason) :o

Thanks. (It's tired and I'm getting very late)

    Why not do it all in the SQL query itself? You can use mathematical expressions like "time_col + INTERVAL 45 MINUTE" in MySQL queries.

      echo date('H:i',strtotime($time."+{$mins}min")); Will do the trick. Ceers

        If you want to keep the seconds: echo date('H:i:s',strtotime($time."+{$mins}min"));

          Thanks for both responses!

          I am a fan of having the Query do as much of the work as possible, so I tried bradgrafelman's suggestion...but could not QUITE get it to work as is, but with a little more digging (not so tired this morning) I came up with this, which is along the same lines, and it does seem to work.

          ADDTIME(LUNCHBREAK, sec_to_time(LUNCHMINS*60)) AS LUNCH_RTN

          (It's a page for contracted workers who have different numbers of minutes for lunch depending on the length of their work day or nature of their contract. If the user changes his/her lunch hour for a given day, they don't also have to change the return time, the expected return from lunch break will be calculated/displayed)

            Although I was able to add the following to my Query to also create reformatted time display strings for display, it did not seem to work "on the fly" for the calculated (return from lunch) time. So I also used a variation of crsland's suggestion for that value in the display.

            $fR=date('g:iA',strtotime($line[LUNCH_RTN]));

            Thanks again!

              $date = '2011/12/31';// current date
              $date = strtotime(date("Y-m-d", strtotime($date)) . " +1 min");
              echo date("Y-m-d",$date);

                Write a Reply...