robcrozier Posted February 25, 2008 Share Posted February 25, 2008 Hi, what i'm trying to do is add a given duration (as supplied by a user via a form field) - i.e. 3days, to the current time. So basically, i want to take the current time, add the specified duration to it (e.g. 3 days) and then return the output date in this format: Y-m-d H:i:s. I'm getting really confused and just can't figure it out. Here's what i'm currently using, though it's outputting completely the wrong date+time. $current_time = date('Y-m-d H:i:s'); $duration_timestamp = $duration * 86400; $now = convert_datetime_db_format($current_time); // function that converts to timestamp $end_datetime = $now + duration_timestamp; $end = date('Y-m-d H:i:s', $end_datetime); // final date = WRONG!!! Any help appreciated! cheers! Quote Link to comment https://forums.phpfreaks.com/topic/92895-time-calculation/ Share on other sites More sharing options...
uniflare Posted February 25, 2008 Share Posted February 25, 2008 this is simple to do. Make sure <?php $current_time = time(); $duration_timestamp = $duration * 86400; // Amount of days in Seconds (Correct) $end_datetime = $current_time + $duration_timestamp; $end = date('Y-m-d H:i:s', $end_datetime); // final date ?> hope this helps. (time() function gives the current unix timestamp) Quote Link to comment https://forums.phpfreaks.com/topic/92895-time-calculation/#findComment-475868 Share on other sites More sharing options...
robcrozier Posted February 25, 2008 Author Share Posted February 25, 2008 Thanks uniflare, that's done the trick! Quote Link to comment https://forums.phpfreaks.com/topic/92895-time-calculation/#findComment-475874 Share on other sites More sharing options...
cooldude832 Posted February 25, 2008 Share Posted February 25, 2008 strtotime is great for adding +1 hour, +1 Day, etc <?php $3days = date("U",strtotime("+3 days")); $2years = date("U",strtotime("+2 years")); Quote Link to comment https://forums.phpfreaks.com/topic/92895-time-calculation/#findComment-475881 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.