Hi, I get this code somewhere from the web and wanted to implement it on my website.
This code is basicly doing something like facebook's posting time.

But in the code, there is the $session_time that i need to set first.
How do i do this using the PHP?

Thank You for helping :)

Dani AI

Generated

— $session_time needs to be a Unix timestamp (integer seconds since the Unix epoch) that represents the moment you want to compare to now (for example a post’s creation time or a user’s last-activity time). The code you posted expects that numeric timestamp, not a formatted date string.

Common ways to obtain that value:

  • From a database DATETIME/TIMESTAMP: return the Unix timestamp in SQL with UNIX_TIMESTAMP(created_at) or convert in PHP with strtotime($row['created_at']) and cast to int.
  • When creating a new record: store time() (or the SQL UNIX_TIMESTAMP()) as the created-at value.
  • For session activity tracking: set $_SESSION['last_activity'] = time(); when the user acts and use that value for comparisons.

Use a function that returns a string (do not echo from inside) and prefers floor() to avoid awkward rounding. Example (cleaner than echoing inside the helper and avoids round() behaviour):

function time_ago(int $ts): string {
    $now = time();
    if ($ts > $now) {
        $diff = $ts - $now;
        return $diff < 60 ? 'in a few seconds' : 'in the future';
    }
    $diff = $now - $ts;
    if ($diff < 60) return $diff === 1 ? '1 second ago' : "{$diff} seconds ago";
    $mins = floor($diff / 60);
    if ($mins < 60) return $mins === 1 ? '1 minute ago' : "{$mins} minutes ago";
    $hours = floor($diff / 3600);
    if ($hours < 24) return $hours === 1 ? '1 hour ago' : "{$hours} hours ago";
    $days = floor($diff / 86400);
    if ($days < 7) return $days === 1 ? '1 day ago' : "{$days} days ago";
    $weeks = floor($diff / 604800);
    if ($weeks < 4) return $weeks === 1 ? '1 week ago' : "{$weeks} weeks ago";
    $months = floor($diff / 2629746);
    if ($months < 12) return $months === 1 ? '1 month ago' : "{$months} months ago";
    $years = floor($diff / 31556952);
    return $years === 1 ? '1 year ago' : "{$years} years ago";
}

Notes and cautions: keep server and DB timezones consistent (storing UTC is simplest), validate that the timestamp is numeric and in range, and prefer storing timestamps as integers or as UTC DATETIME. For localization, formatting, or more accurate month/year math, use a date library (for example Carbon).

oh sorry,I forgot to put the code which I got from the net,
this is the code :

<?php
function time_stamp($session_time) 
{ 
 
$time_difference = time() - $session_time ; 
$seconds = $time_difference ; 
$minutes = round($time_difference / 60 );
$hours = round($time_difference / 3600 ); 
$days = round($time_difference / 86400 ); 
$weeks = round($time_difference / 604800 ); 
$months = round($time_difference / 2419200 ); 
$years = round($time_difference / 29030400 ); 

if($seconds <= 60)
{
echo"$seconds seconds ago"; 
}
else if($minutes <=60)
{
   if($minutes==1)
   {
     echo"one minute ago"; 
    }
   else
   {
   echo"$minutes minutes ago"; 
   }
}
else if($hours <=24)
{
   if($hours==1)
   {
   echo"one hour ago";
   }
  else
  {
  echo"$hours hours ago";
  }
}
else if($days <=7)
{
  if($days==1)
   {
   echo"one day ago";
   }
  else
  {
  echo"$days days ago";
  }


  
}
else if($weeks <=4)
{
  if($weeks==1)
   {
   echo"one week ago";
   }
  else
  {
  echo"$weeks weeks ago";
  }
 }
else if($months <=12)
{
   if($months==1)
   {
   echo"one month ago";
   }
  else
  {
  echo"$months months ago";
  }
 
   
}

else
{
if($years==1)
   {
   echo"one year ago";
   }
  else
  {
  echo"$years years ago";
  }


}
 


} 
$session_time ="1264326122";
//$session_time=time();

echo "<h1>"; 
echo time_stamp($session_time);
echo "</h1>"; 

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.