Jump to content

Subtract timestamp time from current time


Matic
Go to solution Solved by jazzman1,

Recommended Posts

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 ???

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • Solution

Wow....this is so complex for my old head :)

Everything you need to do is converting the data/time string to an unix timestamp integer number.

I was thinking for something simple like:

$cur_TimeStr="2013-09-02 22:05:39";
$last_TimeStr = "2013-09-01 22:05:39";
$TimeInt = strtotime($cur_TimeStr) - strtotime($last_TimeStr);
$hrs = $TimeInt / 3600;
echo $hrs; // 24
Edited by jazzman1
Link to comment
Share on other sites

great, I get the part where you convert to strtotime format which is the number of seconds since January 1 1970 00:00:00 UTC etc... Then you divide by 3600 to get hours...

 

Problem is I don't know hot to convert this to my subtracted result back... As it is now, it only shows the hours since January 1 1970 00:00:00...

Link to comment
Share on other sites

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.