Page 1 of 1

Addition

Posted: Sun Mar 18, 2007 12:20 pm
by aceconcepts
Hi,

How can i increment a time by 5minutes?

e.g. how do i add 5minutes to a time such as 0800?

I want the results to be 0805.

Thanks.

Posted: Sun Mar 18, 2007 12:21 pm
by feyd

Code: Select all

[feyd@home]>php -r "echo '0800' + 5;"
805

Posted: Sun Mar 18, 2007 1:43 pm
by Christopher
Untested and the hard way:

Code: Select all

function add_minutes_to_time($time, $delta) {
     if (strlen($time) == 4) {
          $dminutes = $delta % 60;
          $dhours = (int)($delta / 60);
          if ($dhours > 23) {
               $dhours = $dhours % 24;
          }
          $hour = substr($time, 0, 2);
          $minute = substr($time, 2, 2);
          $minute += $dminutes;
          if ($minute > 59) {
               $minute -= 60;
               ++$hour;
          }
          $hour += $dhours;
          if ($hour > 23) {
               $hour -= 24;
          }
          return sprintf('%02u%02u', $hour, $minute);
     }
     return '';
}

Posted: Sun Mar 18, 2007 3:21 pm
by alex.barylski
Depends on the format of the TIME I guess. If it's a timestamp then simple math is all that is needed.

Code: Select all

$time = $time + ($mins*60);
If it's not a timestamp, then convert it to one first and perform basic arithmetic - thats what I would do :)

Posted: Sun Mar 18, 2007 5:30 pm
by volka
I like strtotime

Code: Select all

$ts = strtotime('0800 + 5 minutes');
echo date('Y-m-d H:i:s', $ts);