Jump to content

  • Log In with Google      Sign In   
  • Create Account


Subtract timestamp time from current time


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 future_man   Members   -  Reputation: 124

Like
0Likes
Like

Posted 01 September 2013 - 02:09 PM

I want to subtract database timestamp from current server time. The formatted time that is returned to me from my database is "2013-09-01 22:05:39" and I use function date("Y-m-d H:i:s"); so that my server time I get in php is in the same format.

 

How can I get a result in hours? Example: 2013-09-01 22:05:39  minus 2013-09-02 22:05:39 equals 24 hours ???



Sponsor:

#2 frob   Moderators   -  Reputation: 18960

Like
0Likes
Like

Posted 01 September 2013 - 02:38 PM

The php DateTime object has a diff function that subtracts the two, and takes into account things like daylight saving time and other fun quirks of time.




One funny thing about time, the math doesn't always work out that two consecutive days are 24 hours apart.

Daylight Saving Time means sometimes subtracting a day means 25 hours, sometimes 23 hours. The leap-day held every 4 years is predictable, but leap-seconds that keep clocks coordinated are not predictable. If you use sub-second increments there are small adjustments that happen frequently.
Check out my personal indie blog at bryanwagstaff.com.

#3 future_man   Members   -  Reputation: 124

Like
0Likes
Like

Posted 01 September 2013 - 02:41 PM

I looked into this diff function already but how do I use it?



#4 future_man   Members   -  Reputation: 124

Like
0Likes
Like

Posted 01 September 2013 - 03:49 PM

OK I solved my problem like this:

$current_time = date_create(date("Y-m-d H:i:s"));
$last_time = date_create(date("Y-m-d H:i:s", strtotime(get_last_update_time($user_name))));
$result = date_diff($current_time, $last_time);

echo $result->format('%Y-%m-%d %H:%i:%s');

but it echoes my result like so: "00-0-0 03:40:56", with all the days etc.. How do I format this so that it only show hours, lets say like so: "232156". With all the days, months years, formatted to hours?



#5 Dennis Technologies   Members   -  Reputation: 147

Like
0Likes
Like

Posted 06 September 2013 - 09:44 AM

The php manual is awesome for this. date_diff returns either a DateInterval object or a boolean.

To calculate just hours I would use this formula: hours = (days * 24) + (h) + (i / 60) + (s / 3600)

Some notes:
1. Test for failure before using the DateInterval or you might throw exceptions.
2. DateInterval-> days is an integer rounded down.
3. Don't do the division in a loop. Precalc to a constant and multiply.


EDIT: Also if you are looking for straight linear time diffs then I would avoid timestamps in date formats all together since you can run into funky 'leap' issues. Store your timestamps in milliseconds since epoch and divide from there.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS