HI everyone!!i have this got this function, and i dont know what is going wrong here.
in the table, the field for date i saved as timestamp, meaning time is automatically inserted when the record inters.

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";
  }


}
 


} 

//here is how i`m calling it 

$time=time_stamp($session_time);

The result it is giving me is 44 years ago,while i just inserted the record not long ago 2010-03-03 00:01:51

So i`m wondering if somebody can tell me how to call this function or in which format should i save the date field.

Recommended Answers

All 4 Replies

have you tried

time_stamp(time());

And made sure that $session_time is a properly formatted UNIX timestamp?

2010-03-03 00:01:51 is in mysql timestamp format

PHP timestamp format is a number representing the number of seconds passed since 1970-01-01 00:00:00
you can't substract a mysql timestamp from a php one. It's like adding 2 pears + 4 apples = 6 WHAT? peapples :)

function transform_ts($mysql_ts)
{
$hour = substr($mysql_ts,11,2);
$minute = substr($mysql_ts,14,2);
$second = substr($mysql_ts,17,2);
$month = substr($mysql_ts,5,2);
$day = substr($mysql_ts,8,2);
$year = substr($mysql_ts,0,4);
$mktime = mktime($hour, $minute, $second, $month, $day, $year);
return $mktime;
}

Then when calling your function:

$time=time_stamp(transform_ts($session_time));

PHP timestamp format is a number representing the number of seconds passed since 1970-01-01 00:00:00
you can't substract a mysql timestamp from a php one. It's like adding 2 pears + 4 apples = 6 WHAT? peapples

EPIC COMPARISON
*stolen* =)

@alex.tepes
i didn`t know if it needs another function for this to work.
Thank You,now everything is Fine.

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.