Adding Two Time Values of Similar Formats Using PHP

adding two time values of similar formats using php

this code sample would take hour in $time and add the hour in $time2 to it

for example: time=06:58:00, time2=00:40:00, result = 07:38:00

$time = "06:58:00";
$time2 = "00:40:00";

$secs = strtotime($time2)-strtotime("00:00:00");
$result = date("H:i:s",strtotime($time)+$secs);

PHP add up two time variables

The best way to do this is most likely to use strtotime to convert them to timestamps and then do the adding together:

$o = strtotime($time1)+strtotime($time2);

If I remember right strtotime does support this format.

Otherwise you will need to filter it out yourself.

Adding two time values in php

You should do this:

<?
echo date("H:i:s", strtotime("06:30:00 + 3 hour"));
echo date("H:i:s", strtotime("06:30:00 - 3 hour"));
?>

Adding two times together in php

This seems to be what you're after:

<?php
$period = new DatePeriod(
new DateTime('06:05:00'),
DateInterval::createFromDateString('15 minutes'),
new DateTime('07:00:00'));
foreach($period as $interval){
echo $interval->format('c').PHP_EOL;
}

Result:

2013-04-22T06:05:00+02:00
2013-04-22T06:20:00+02:00
2013-04-22T06:35:00+02:00
2013-04-22T06:50:00+02:00

You could also use new DateInterval('PT15M'), or more formally from a time:

new DateInterval('PT'.$everyMinutesRaw->format('H\Hi\Ms\S'));

If you're not interested in all the intervals but just want the first one as per your example:

 $startTimeRaw->add(new DateInterval('PT'.$everyMinutesRaw->format('H\Hi\Ms\S')));
echo $startTimeRaw->format('H:i:s');

Adding two time values in php

You should do this:

<?
echo date("H:i:s", strtotime("06:30:00 + 3 hour"));
echo date("H:i:s", strtotime("06:30:00 - 3 hour"));
?>

How to add different timestamp

You can't add time like they are integer. One of the way is to convert it to another format and then add it. After adding them, convert it back to time type.

For example, try this:

$time1 = "00:30:00";
$time2 = "00:45:31";

$secs = strtotime($time2) - strtotime("00:00:00");
$result = date("H:i:s", strtotime($time1) + $secs);

dd($result); // "01:15:31"

add two time values and comparing with other time

If you want to echo your $add variable in a readable form you have to format it first.

echo(date("H:i:s", $add));

how to add two time more than 24 hours

You have a logical error in your code, you just simply sum every part of the time but the thing you need to do is this :

If number of seconds is superior to 60 you have to get only the result of $number_seconds%60 and add it to your minutes and repeat it with the minutes, let me show you :

       function TimeSum($a,$b)
{
list ($hour1, $min1, $sec1) = explode(':', $a);
list ($hour2, $min2, $sec2) = explode(':', $b);
//counting number of seconds and getting extra minutes outs
$total_sec = $sec1+$sec2;
$sumSec = $total_sec%60;
$extra_min = ($total_sec-$sumSec)/60;
//counting number of minutes and getting extra hours outs
$total_min= $min1+$min2+$extra_min;
$sumMin = $total_min%60;
$extra_hr = ($total_min-$sumMin)/60;
//counting number of hours
$sumHour = $hour1 + $hour2 + $extra_hr;

return $sumHour.':'.$sumMin.':'.$sumSec;
}

add two or more time strings in php

There is no builtin way of doing this - all time functions operate on times,
not on durations. In your case, you can explode() and add the parts
separately. I would recommend writing a class. Simple example:

class Duration {

public static function fromString($string) {
$parts = explode(':', $string);
$object = new self();
if (count($parts) === 2) {
$object->minutes = $parts[0];
$object->seconds = $parts[1];
} elseif (count($parts) === 3) {
$object->hours = $parts[0];
$object->minutes = $parts[1];
$object->seconds = $parts[2];
} else {
// handle error
}
return $object;
}

private $hours;
private $minutes;
private $seconds;

public function getHours() {
return $this->hours;
}

public function getMinutes() {
return $this->minutes;
}

public function getSeconds() {
return $this->seconds;
}

public function add(Duration $d) {
$this->hours += $d->hours;
$this->minutes += $d->minutes;
$this->seconds += $d->seconds;
while ($this->seconds >= 60) {
$this->seconds -= 60;
$this->minutes++;
}
while ($this->minutes >= 60) {
$this->minutes -= 60;
$this->hours++;
}
}

public function __toString() {
return implode(':', array($this->hours, $this->minutes, $this->seconds));
}

}

$d1 = Duration::fromString('2:22');
$d1->add(Duration::fromString('3:33'));
echo $d1; // should print 5:55


Related Topics



Leave a reply



Submit