Jump to content

Subtract timestamp time from current time


Matic

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

 

This is not unix timestamp format, the "timestamp" is just a big integer.

You can "google" how to convert this data/time format to unix timestamp and then apply this subtraction.

You could also consider using  Data/Time MySQL Functions

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?

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

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

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.