Chinmay235
Chinmay235

Reputation: 3414

How to sum hours with existing datetime in PHP?

I have two fields which store data like 2018-03-26 11:20:35 and 02:25:10(2 hours 25 minutes and 10 seconds) first data is date and time. second one is only time. I want to sum it and finally my result should 2018-03-26 13:45:45

How to do that in php code?

I have tried this way:

<?php
$date = '2018-03-26 11:20:35';
//echo $date;
//echo "<br>";
$hours = '02:25:10'; /* this data dynamic */
$sumTime = strtotime($date) + strtotime($hours);
$new_time = date("Y-m-d H:i:s", $sumTime);
echo $new_time;

Output:

Warning: date() expects parameter 2 to be integer, float given in C:\my-project-path\test.php on line 7

Upvotes: 1

Views: 3388

Answers (5)

Glavić
Glavić

Reputation: 43552

It could be done with some simple string manipulation:

$dt = new DateTime("$date UTC");
$modify = preg_replace('/:/', ' hours ', $hours, 1);
$modify = preg_replace('/:/', ' minutes ', $modify, 1);
$modify .= ' seconds';
$dt->modify($modify);

demo

If you have MySQL as your data storage, you could do:

DATE_ADD(field1, INTERVAL field2 HOUR_SECOND)

demo

Upvotes: 2

Pedro Amaral Couto
Pedro Amaral Couto

Reputation: 2115

You should check DateTime::add:

Example:

<?php

// Convert h:m:s format to PThHmMsS format
sscanf('02:25:10', '%d:%d:%d', $hour, $minute, $second);
$intervalSpec = sprintf('PT%dH%dM%dS', $hour, $minute, $second);

$datetime = new DateTimeImmutable('2018-03-26 11:20:35');
$newDatetime = $datetime->add (new DateInterval($intervalSpec));
echo $newDatetime->format(DateTime::W3C);

Upvotes: 2

Syscall
Syscall

Reputation: 19780

You could create a duration in seconds by comparing today at "00:00:00" and today at $hours. Actually, strtotime($hours) returns the timestamp of today at $hours, so, the addition of the two timestamp don't give the expected result.

If $hours is lesser than 24 hours, you could use:

$date = '2018-03-26 11:20:35';
$hours = '02:25:10';

$d0 = strtotime(date('Y-m-d 00:00:00'));
$d1 = strtotime(date('Y-m-d ').$hours);

$sumTime = strtotime($date) + ($d1 - $d0);
$new_time = date("Y-m-d H:i:s", $sumTime);
echo $new_time; 

Outputs:

2018-03-26 13:45:45

Upvotes: 3

Lilian Barraud
Lilian Barraud

Reputation: 337

you can do something like:

$hour = $hours->format('H'); //This way you get a string which contains the hours
$date->modify('+'.$hour.' hour'); //you modify your date adding the hours

I'm assuming you only need the hours, and not minutes and seconds

EDIT:

you can do like that using regexp

$date = new \DateTime('2018-03-26 11:20:35');
$hours ='02:25:10';
preg_match("/^([0-9].*):([0-9].*):([0-9].*)/",$hours,$matches);
$date->modify('+'.$matches[1].' hour');
$date->modify('+'.$matches[2].' minute');
echo $date->modify('+'.$matches[3].' second')->format('Y-m-d H:i:s');

Upvotes: 1

u_mulder
u_mulder

Reputation: 54831

Here's a simple solution, some checks are skipped:

// convert your date to DateTime object
$date = '2018-03-26 11:20:35';
$dt = new DateTime($date);

// convert your period to DateInterval
$hours = '02:25:10'; /* this data dynamic */
$parts = explode(':', $hours);
$interval = new DateInterval('PT' . (int)$parts[0] . 'H' . $parts[1] . 'M' . $parts[2] . 'S');

// Add interval to date
$dt->add($interval);
// Format date as you need
echo $dt->format('Y-m-d H:i:s');

Upvotes: 5

Related Questions