Jump to content

[SOLVED] Find time between CURENT time and a timestamp in a DB..


lynxus

Recommended Posts

Hi guys,

 

How can i echo the amount of time passed between a database timestamp and the current time.

 

IE:

select time from table limit 1;

= 2009-08-28 09:55:45

 

How can i do:

 

Time since last time = XXHours, XXminutes, XXSeconds.

 

IE: the time between the above time and the current server time if this makes any sense?

 

 

Thanks in advance.

 

Graham

Link to comment
Share on other sites

Hi, Thanks for your reply.

 

Im confused?

How is that going to tell me how much time has passed since the timestamp?

I probably didnt make my question clear enough.

 

Lets say

$lasttime = "2009-08-28 09:55:45";

 

How can i say.

XDays, Xhours, Xseconds has passed since $lasttime;

 

Thanks

G

Link to comment
Share on other sites

Humm,

getting closer :)

 

this is what i have from my DB result:

 

$result = mysql_query("SELECT time FROM visitorips where siteid = '$siteid' order by id DESC limit 1");

 

while($row = mysql_fetch_array($result))

  {

  $diff = time() - $row['time'];

$lasttime = strftime('%e days %H hours %M minutes %S seconds', $diff);

}

 

( Result = 2009-08-28 11:12:47 )

 

However when i echo $lasttime i get:

28 days 10 hours 50 minutes 51 seconds

 

Something must be wrong here? but im not sure what.

the system time is correct as the time inserted to the DB is from the system time..

 

I would of thought it would say something like 0days 12minutes xx seconds ?

 

Any ideas?

Thanks

Graham

 

Link to comment
Share on other sites

hheh, thanks,

Getting closer.

 

Seems to still be quite far out.

 

while($row = mysql_fetch_array($result))
  {
  $diff = time() - strtotime($row['time']);
  echo $row['time'];
  echo "<br>";
  echo time();
  echo "<br>";
  echo strtotime($row['time']);
$lasttime = strftime('%ed %Hh %Mm %Ss ago', $diff);
}

 

Result:

2009-08-28 13:22:44

1251462463

1251462164

 

Showing : 1d 01h 05m 44s ago

 

What could be wrong here?

Last hit shows to be around 10minutes ago.

 

Thanks again

Graham

Link to comment
Share on other sites

I already guessed that these wouldn't work. Then the normal way I guess:

 

$lasttime = strtotime($row['time']);
$diff = time() - $lasttime;

$days = floor($diff / 86400);
$hours = floor(($diff % 86400) / 3600);
$minutes = floor((($diff % 86400) % 3600) / 60));
$seconds = floor(((($diff % 86400) % 3600) % 60)));

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.