Backend Development

[PHP & TimeStamp] Time Ago

Submitted by Gashi, , Thread ID: 22918

Thread Closed
31-08-2016, 05:01 AM
This post was last modified: 31-08-2016, 05:02 AM by Gashi
#1
Through this code you can set any code for Time Well to show something like: 1 second ago, 5 minutes ago, 3 weeks ago, six months ago or eight years ago.

Date and time format of the jet should thus: 2016-08-28 15:47:01

Date&Time Format For PHP: date("Y-m-d h:i:sa");

File: timeago.php


Code:
<?php
function time_stamp($time_ago)
{
$cur_time=time();
$time_elapsed = $cur_time - $time_ago;
$seconds = $time_elapsed ;
$minutes = round($time_elapsed / 60 );
$hours = round($time_elapsed / 3600);
$days = round($time_elapsed / 86400 );
$weeks = round($time_elapsed / 604800);
$months = round($time_elapsed / 2600640 );
$years = round($time_elapsed / 31207680 );
// 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 "an hour ago";
  }
  else
  {
  echo "$hours hours ago";
  }

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

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

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

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

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

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

}
//Years
else
{

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

}

}
?>
File: index.php
Code:
<?php
    include 'timeago.php';
    $time="2016-08-28 15:47:01";
    $time_ago =strtotime($time);
     echo time_stamp($time_ago);
     ?>

RE: [PHP & TimeStamp] Time Ago

#2
As a PHP noob, I was just about to post a thread asking if you could get a result returned from another php script, but your post has answered the question without me having to post. Thanks very much.

RE: [PHP & TimeStamp] Time Ago

#3
my god, thank you, this is a really good thing for a website or forum

RE: [PHP & TimeStamp] Time Ago

#4
Thanks for the code, it's not advanced but thanks!

Users browsing this thread: 1 Guest(s)