Jump to content

time calculation!


robcrozier

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/92895-time-calculation/
Share on other sites

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)

Link to comment
https://forums.phpfreaks.com/topic/92895-time-calculation/#findComment-475868
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.