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.
Addition
Moderator: General Moderators
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
[feyd@home]>php -r "echo '0800' + 5;"
805- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
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 '';
}(#10850)
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Depends on the format of the TIME I guess. If it's a timestamp then simple math is all that is needed.
If it's not a timestamp, then convert it to one first and perform basic arithmetic - thats what I would do 
Code: Select all
$time = $time + ($mins*60);I like strtotime
Code: Select all
$ts = strtotime('0800 + 5 minutes');
echo date('Y-m-d H:i:s', $ts);