Jump to content

Recommended Posts

Probably something simple but I have searched high and low and can't figure this one out.

 

I have a variable that is of the datetime format.

I have another variable that is of the time format.

I need to add them together.

 

Example:

$var1 = 2012-02-24 06:38:22

$var2 = 02:00:00

 

$var3 = $var1 + $var2 = 2012-02-24 08:38:22

 

Thanks for the help!

Link to comment
https://forums.phpfreaks.com/topic/257698-add-time-variable-to-datetime-variable/
Share on other sites

You would need to convert one or the other, so that they are both the same format.

 

You can take a look at my post that I wrote on handling dates in PHP.  I still have a few more to talk about from this topic, but that is a start.

 

Handling dates in PHP - Jotorres Web development

If you are pulling these from a mySql database, you can add them in the SELECT query using the ADDTIME() function

 

 SELECT StartTime, Duration, ADDTIME(StartTime, Duration) AS EndTime ...

 

If you really need to add them in PHP, you can use the strtotime function on each of them, add the results together, then use the date function to reformat them.

 

$var1 = '2012-02-24 06:38:22';
$var2 = '02:00:00';

$t1 = strtotime($var1);
$t2 = strtotime($var2, 0); // NOTE the 0 (zero) so we don't get the current date
$t3 = $t1 + $t2;

$var3 = date('Y-m-d H:i:s', $t3);

print($var3 . PHP_EOL);  // Output: 2012-02-24 08:38:22

There are 2 fields in a form.  One to capture start time and the other to capture duration.

 

So here is what I have.

 

$time_start = '2012-20-24 03:30:00';
$duration = '02:00:00';

$time_finish = date('Y-m-d H:i:s', (strtotime($time_start) + strtotime($duration,0)));

echo $time_finish;

 

This gives me 2012-02-23 10:30:00.

Goes back in time?

 

Damn Timezones! :suicide:

 

We have to treat them all as UTC times to get this to work correctly. You might want to (eventually) look at the PHP DateTime class.

 

// Assuming we have a timezone set ...
date_default_timezone_set('America/Chicago');

$var1 = '2012-02-24 06:38:22';
$var2 = '02:00:00';

$t1 = strtotime($var1 . ' UTC');  // CHANGED THIS
$t2 = strtotime('1970-01-01 ' . $var2 . ' UTC'); // AND CHANGED THIS (don't know why zero wouldn't work here)
$t3 = $t1 + $t2;

$var3 = gmdate('Y-m-d H:i:s', $t3);  // AND CHANGED THIS

print($var3 . PHP_EOL);  // Output: 2012-02-24 08:38:22

Hey Guys thanks for all the help.

I figured it out by creating a function to change my time to seconds, then they added just fine.

Plus figured the function could be useful in the future.

Here's the result...

 


//time to seconds of the format hh:mm:ss
  function time_to_sec($time) {
    $t = explode(':', $time);
    $in_sec = $t[0] * 3600 + $t[1] * 60 + $t[3];
    
    return $in_sec;
  }

$time_finish = date('Y-m-d H:i:s', (strtotime($time_start) + time_to_sec($duration)));

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.