Forum Moderators: coopster

Message Too Old, No Replies

time ago function

         

NeilsPHP

9:57 pm on Oct 22, 2008 (gmt 0)

10+ Year Member



i am trying to develop a site that will have a timestamp function similar to "2hours 30 min ago" last activity was done by user(or 20 days and 50 min ago...if more days have passed).I could not find much suitable for what I need,but here is what I found and made few changes to get what I need.
1.I am accessing MYSQL table column for a date
2.use this date as previous(p suffix) date
3.use c suffix for current date
4.I am doing some calculation on the two,and the result I am getting is working.ONly problem is..I can not get it working real-time..what I mean is I can not refresh the page and get the new time difference updated..Even though I close the browser and open another one..it basically takes the instance of data acquired from db and current time that time and kinda "locks" into it.(until I provide different values from mysql)
any help would be appreciated to show current timestamp refreshed..or diff way of doing it.
here is code:

function connect_db.php
$query_Q = mysql_query ("SELECT * FROM TABLE WHERE id='2'") or die(mysql_error());
if (mysql_num_rows($query_Q) > 0)
{
list ($id,$p_h,$p_m,$p_s,$p_d,$p_mo,$p_y) = mysql_fetch_row($query_Q) ;

///THIS IS WHERE I M GETTIN CURRENT DATE
$c_y = date("Y");
$c_mo = date("m");
$c_d = date("d");

$c_h = date("H", time());
$c_m = date("i", time());
$c_s = date("s", time());

$seconds_dif = mktime("$p_h,$p_m,$p_s,$p_mo,$p_d,$p_y")-mktime("$c_h,$c_m,$c_s,$c_mo,$c_d,$c_y") ;

$Y = floor($seconds_dif/365/60/60/24);
$MON = floor($seconds_dif/60/60/24/7/4);
$W = floor($seconds_dif/60/60/24/7);
$D = floor($seconds_dif/60/60/24);
$H = floor($seconds_dif/60/60);
$MIN = floor($seconds_dif/60);
$SEC = $MIN*60 ;

echo "$seconds_dif <br>";
echo "$Y <br>";
echo "$MON <br>";
echo "$W <br>";
echo "$D <br>";
echo "$H <br>";
echo "$MIN <br>";
echo "$SEC <br>";

$total_hrs = ($H-($D*24));
$total_min = ($MIN-($H*60));
$total_sec = ($SEC-($MIN*60));

if($D > 6)
{echo"$W week(s) ago";}
elseif(D>1)
{echo"$D days,$total_hrs hours ago";}
elseif($D==1)
{echo"1 day $total_hrs hours ago";}
elseif($H>1)
{echo"$H hours $total_min minutes ago";}
elseif($H==1)
{echo"1 hour $total_sec seconds ago";}
elseif($H<1)
{echo"$total_min minutes $total_sec seconds ago)";}

}

THANKS

daveginorge

6:55 am on Oct 23, 2008 (gmt 0)

10+ Year Member



Hi

You have your $SEC updating from $MIN * 60 which means that the $SEC will only update every minute $SEC is actually the same as $seconds_dif. So $SEC = $seconds_dif

I had to make changes to the code as I do not have access to your db but this code works and updates each refresh. If your problem was the seconds not updating then that was solved above.

$seconds_dif = mktime($c_h,$c_m,$c_s,$c_mo,$c_d,$c_y) - mktime(8, 15, 30, 10, 23, 2008) ;

$Y = floor($seconds_dif/365/60/60/24);
$MON = floor($seconds_dif/60/60/24/7/4);
$W = floor($seconds_dif/60/60/24/7);
$D = floor($seconds_dif/60/60/24);
$H = floor($seconds_dif/60/60);
$MIN = floor($seconds_dif/60);
$SEC = $seconds_dif ;

echo "Current date = " . date('r');
echo "Secs = $seconds_dif <br>";
echo "Year = $Y <br>";
echo "Month = $MON <br>";
echo "Week = $W <br>";
echo "Day = $D <br>";
echo "Hour = $H <br>";
echo "Min = $MIN <br>";
echo "Sec = $SEC <br>";

HTH
Dave

daveginorge

9:12 am on Oct 23, 2008 (gmt 0)

10+ Year Member



You can add a little extra.

// Seconds in:
$Ysec = 60 * 60 * 24 * 365;
$Msec = 60 * 60 * 24 * 30;
$Wsec = 60 * 60 * 24 * 7;
$Dsec = 60 * 60 * 24;
$Hsec = 60 * 60;
$Minsec = 60;

$Y = (int)($seconds_dif / $Ysec);
$seconds_dif = $seconds_dif % $Ysec;

$MON = (int)($seconds_dif / $Msec);
$seconds_dif = $seconds_dif % $Msec;

$W = (int)($seconds_dif / $Wsec);
$seconds_dif = $seconds_dif % $Wsec;

$D = (int)($seconds_dif / $Dsec);
$seconds_dif = $seconds_dif % $Dsec;

$H = (int)($seconds_dif / $Hsec);
$seconds_dif = $seconds_dif % $Hsec;

$MIN = (int)($seconds_dif / $Minsec);
$seconds_dif = $seconds_dif % $Minsec;

$SEC = $seconds_dif ;

echo "Welcome back<br>you last logged in: ";
if($Y > 0 ) echo "$Y year ";
if($MON > 0) echo "$MON months ";
if($W > 0) echo "$W weeks ";
if($D > 0) echo "$D days ";
if($H > 0) echo "$H hours ";
if($MIN > 0) echo "$MIN minutes ";
echo "$SEC seconds ";
echo "ago";

Dave

NeilsPHP

6:39 pm on Oct 23, 2008 (gmt 0)

10+ Year Member



Dave,
Thank you so much !
I see my problem with SEC not updating..it works now..I also liked your second idea..and I am going to actualy use it (after customizing a little)
Greatly appreciate it.