dmhall0 Posted February 24, 2012 Share Posted February 24, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/257698-add-time-variable-to-datetime-variable/ Share on other sites More sharing options...
jotorres1 Posted February 24, 2012 Share Posted February 24, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/257698-add-time-variable-to-datetime-variable/#findComment-1320768 Share on other sites More sharing options...
DavidAM Posted February 24, 2012 Share Posted February 24, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/257698-add-time-variable-to-datetime-variable/#findComment-1320851 Share on other sites More sharing options...
dmhall0 Posted February 24, 2012 Author Share Posted February 24, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/257698-add-time-variable-to-datetime-variable/#findComment-1320879 Share on other sites More sharing options...
DavidAM Posted February 24, 2012 Share Posted February 24, 2012 Damn Timezones! 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 Quote Link to comment https://forums.phpfreaks.com/topic/257698-add-time-variable-to-datetime-variable/#findComment-1320894 Share on other sites More sharing options...
dmhall0 Posted February 24, 2012 Author Share Posted February 24, 2012 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))); Quote Link to comment https://forums.phpfreaks.com/topic/257698-add-time-variable-to-datetime-variable/#findComment-1320900 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.