IDIOT DEVELOPER

Time Stamp Function in PHP
Time Stamp Function in PHP
DEMO   DOWNLOAD

Everyone has seen the string 2 days ago, or 1 week ago in social networking websites and other websites. In this PHP tutorial we are going to build this functionality. In short in this tutorial we are going to convert unix timestamp into the time ago string. We are going to create a function in PHP which is going to take the timestamp value , and then subtract it from the current timestamp value, then return the appropriate time ago string.

 

What is Time Stamp

When the date and time of an event is recorded, we say that it is timestamped. A digital camera will record the time and date of a photo being taken, a computer will record the time and date of a document being saved and edited. A social media post may have date and time recorded. These are all examples of a timestamp.

 

PHP Time Stamp Function

The timestamp() function takes the time stamp as a parameter. If the given time stamp value is less than or equal to the current time stamp then the function will continue execution otherwise the function returns the message Given time is greater than the current time and stops. Now the function uses the some set of rules and calculate the seconds, minutes, days, weeks, months or years.

 

The rules are as follows:

 

12 * 30 * 24 * 60 * 60 => ‘year’,
30 * 24 * 60 * 60 => ‘month’,
7 * 24 * 60 * 60 => ‘week’,
24 * 60 * 60 => ‘day’,
60 * 60 => ‘hour’,
60 => ‘minute’,
1 => ‘second’

 

The time stamp is in seconds, so we have to convert the seconds into corresponding units. 1 minutes consists of 60 seconds. An hour consists of 60 minutes. A day consists of 24 hours. A week consists of 7 days. and a month consists of 30 days, and an year consists of 12 months, so thats how you convert those remaining time stamp seconds.

 


<?php
function timestamp($given_time){
  $current_time = time();

  if($current_time >= $given_time){
    $time_diff = $current_time - $given_time;
    $seconds = $time_diff;
    $minutes = round($time_diff / 60);
    $hours = round($time_diff / (60*60));
    $days = round($time_diff / (60*60*24));
    $weeks   = round($time_diff / (60*60*24*7));
    $months = round($time_diff / (60*60*24*30));
    $years = round($time_diff / (60*60*24*30*12));

    if($seconds <= 60){
      $ret = $seconds . ' seconds ago';
    }else if($minutes <= 60){
      $ret = $minutes . ' minutes ago';
    }else if($hours <= 24){
      $ret = $hours . ' hours ago';
    }else if($days <= 7){
      $ret = $days . ' days ago';
    }else if($weeks <= 4){
      $ret = $weeks . ' weeks ago';
    }else if($months <= 12){
      $ret = $months . ' months ago';
    }else{
      $ret = $years . ' years ago';
    }

    return $ret;

  }else{
    return 'Given time is greater than the current time.';
  }
}
?>

How to Use It

First we need to include this PHP file.

 


<?php include_once('time.php'); ?>

 

Then we need to call the timestamp() function, then pass it the time stamp value.

 


<?php
$time = 1506117600; 
timestamp($time);
?>

 

I hope you enjoyed my tutorial. If you like it then share it with your friends. Thank You.

2 comments on “Time Stamp Function in PHP”

Leave a Reply

Your email address will not be published. Required fields are marked *