PHP Time Stamp Function.
Wall Script
Follow Me:
Sunday, January 24, 2010

PHP Time Stamp Function.

I had developed a date time stamp function using PHP and implemented this at labs.9lessons.info. It is very important for social media web applications. I hope it's useful for you.

Facebook Wall Script 2.0 Download link : http://labs.9lessons.info/status.php?msgid=150

PHP Time Stamp Function.

Download Script     Live Demo

time_stamp.php
Contains PHP code. You have to use the time_stamp() function to display the date and time.
<?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 );
// Seconds
if($seconds <= 60)
{
echo "$seconds seconds ago";
}
//Minutes
else if($minutes <=60)
{

   if($minutes==1)
  {
   echo "one minute ago";
   }
   else
   {
    echo "$minutes minutes ago";
   }

}
//Hours
else if($hours <=24)
{

   if($hours==1)
  {
   echo "one hour ago";
  }
  else
  {
   echo "$hours hours ago";
  }

}
//Days
else if($days <= 7)
{

  if($days==1)
  {
   echo "one day ago";
  }
  else
  {
   echo "$days days ago";
   }

}
//Weeks
else if($weeks <= 4)
{

   if($weeks==1)
  {
   echo "one week ago";
   }
  else
  {
   echo "$weeks weeks ago";
  }

}
//Months
else if($months <=12)
{

   if($months==1)
  {
   echo "one month ago";
   }
  else
  {
   echo "$months months ago";
   }

}
//Years
else
{

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

}

}
$session_time ="1264326122";
//$session_time=time();
echo time_stamp($session_time);
?>


Was this article helpful?
Thanks! Your feedback helps us to improve 9lessons.info


40 comments:

  1. Hi,
    i would write this function this way:
    http://nopaste.info/465ae57b7f_nl.html

    1. Use return not echo in a function.
    2. Your formatting was BAD.

    ReplyDelete
  2. vaya, el script es muy util, tiene buena pinta y es justo lo que necesitaba.. muchas gracias chato :)

    ReplyDelete
  3. I don't think timestamp are the way to go. Rather use normale date represetations.

    ReplyDelete
  4. first time such a light post.. you should write something more important.

    ReplyDelete
  5. Here what I wrote long before myspace and facebook.

    http://nopaste.info/5537800c28.html

    ReplyDelete
  6. nice Indian code =)

    Try ZF/Symfony

    ReplyDelete
  7. nice script.. excellent job..!!

    ReplyDelete
  8. Very nice job. You're like a genius at this.

    ReplyDelete
  9. the perfect code thanx Srinivas Tamada ;)

    ReplyDelete
  10. hi guys!here is how i`m saving time in the db,the date field is timestamp.
    but when i try to load it is gives me 44 years ago.

    $session_time="2010-03-03 00:01:51";
    $time=time_stamp($session_time);
    So what i`m i doing wrong here

    ReplyDelete
  11. well i got it working
    $session_time="2010-03-03 00:01:51";
    $time=time_stamp($session_time);

    ReplyDelete
  12. Try with:

    $session_time= strtotime("2010-03-03 00:01:51");
    $time=time_stamp($session_time)

    ReplyDelete
  13. this is a great script...very few websites present realtime examples like this...you are great...keep up good job dude

    ReplyDelete
  14. I would Do it this way


    function timesince($original) {
    $ta = array(
    'year' => 31536000, // year
    'month' => 2592000, // month
    'week' => 604800, // week
    'day' => 86400, // Day
    'hour' => 3600, // Hour
    'minut' => 60, // Minute
    'second' => 1); // Second
    $since = time() - $original;
    $res = '';
    foreach($ta as $key => $value) {
    $cnt = floor($since / $value);
    if($cnt != 0) {
    $since = $since - ($value * $cnt);
    if($res == ''){
    $res .= ($cnt == 1) ? '1 ' . $key : "$cnt {$key}s";
    } else {
    $res .= ($cnt == 1) ? ' 1 ' . $key : " $cnt {$key}s";
    }
    }
    }
    return $res;
    }

    ReplyDelete
  15. A little improvment to the last posted version. Is more a facebook style date:

    function timesince($original) {
    $ta = array(
    array(31536000, "Year"),
    array(2592000, "Month"),
    array(604800, "Week"),
    array(86400, "Day"),
    array(3600, "Hour"),
    array(60, "Minute"),
    array(1, "Second")
    );

    $since = time() - $original;

    $res = "";
    $lastkey = 0;

    for( $i=0; $i= 60 && ($i - $lastkey) == 1 ){
    $res .= ($cnt == 1) ? " y 1 {$ta[$i][1]}" : " y {$cnt} {$ta[$i][1]}s";
    break;
    }else{
    break;
    }
    }
    }

    return $res;
    }

    ReplyDelete
  16. hi ..i need a reverse i mean from "Posted 3 hours 26 mins ago" to timestamp

    ReplyDelete
  17. my cms posts return posted 40 years ago by: Administrator every time i add a new entry instead of showing may b 1 minue ago.
    can some one help me.
    Thanks

    ReplyDelete
  18. what should i do if i have the time saved as "2011-07-10T16:32:24+00:00" for example?

    ReplyDelete
  19. very usefull and pure php code keep going.

    thanks

    ReplyDelete
  20. oh??? i have a problem with it... it shows 9hrs ago and it should me a bit of seconds ago will be display.. so what is the problem???

    ReplyDelete
  21. how can i apply this code to my project?pls help. huhu

    ReplyDelete
  22. Couldn't "one day ago" be "yesterday"?

    ReplyDelete
  23. how do you make it loop. like if you have so many reply's in a post. do you put it in side a "while" loop? thanks

    ReplyDelete
  24. cool article and script... keep up the good work...

    ReplyDelete
  25. Very Helpful Article..

    ReplyDelete
  26. Good work, was very useful to me :)

    ReplyDelete
  27. Excellent Stuff, Working like a charm ! thanks Srinivas

    ReplyDelete

Make in India