In this tutorial you will learn how to convert MySQL Timestamp to Ago Time format using both procedural and Object oriented method.

[embedyt] http://www.youtube.com/watch?v=8wWTMnFm5gM[/embedyt]

Source Code: OOP Style
Ago.php

[php]
?php
class Ago
{
/**
* @param $value
* @return int
* * use PHP list to create required variables
* use the explode function to convert the given date into an array
* and extract desired values into the
* variables created with the list
*/

public function convertToUnixTimestamp($value) {
date_default_timezone_set(‘Asia/Kuala_Lumpur’);
list($date, $time) = explode(‘ ‘, $value);
list($year, $month, $day) = explode(‘-‘, $date);
list($hour, $minutes, $seconds) = explode(‘:’, $time);

/**
* use the make time function to get the Unix timestamp for the given date
*/

$unit_timestamp = mktime($hour, $minutes, $seconds, $month, $day, $year);

return $unit_timestamp;
}

public function convertToAgoFormat($timestamp){
date_default_timezone_set(‘Asia/Kuala_Lumpur’);
$diffBtwCurrentTimeAndTimestamp = time() – $timestamp;
$periodsString = ["sec", "min", "hr", "day", "week", "month", "year", "decade"];
$periodsNumber = ["60", "60", "24", "7", "4.35", "12", "10"];

for($iterator = 0; $diffBtwCurrentTimeAndTimestamp >= $periodsNumber[$iterator]; $iterator++)
$diffBtwCurrentTimeAndTimestamp /= $periodsNumber[$iterator];
$diffBtwCurrentTimeAndTimestamp = round($diffBtwCurrentTimeAndTimestamp);

if($diffBtwCurrentTimeAndTimestamp != 1) $periodsString[$iterator].="s";
$output = "$diffBtwCurrentTimeAndTimestamp $periodsString[$iterator]"; //2 days

return "Posted ".$output." ago";
}
}
[/php]

index.php

[php]
<?php
$posted_at = "2016-1-31 20:17:16"; //now()
include_once ‘Ago.php’;
$ago = new Ago();
$unixTimestamp = $ago->convertToUnixTimestamp($posted_at);
?>;

<div style="width: 350px;">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. <br>;
<span style="float: right; font-size: 10px; color: gray;">;
<?php echo $ago->convertToAgoFormat($unixTimestamp); ?>
</span>;
</div>;
?>
[/php]

[download id=”462″]
Source Code: Procedural Style

[php]&lt;?php
$posted_at = "2016-1-31 20:17:16"; //now()

/**
* @param $value
* @return int
* use PHP list to create required variables
* use the explode function to convert the given data into an array and extract desired values into the
* variables created with the list
*/

function convertToUnixTimestamp($value) {
date_default_timezone_set(‘Asia/Kuala_Lumpur’);
list($date, $time) = explode(‘ ‘, $value);
list($year, $month, $day) = explode(‘-‘, $date);
list($hour, $minutes, $seconds) = explode(‘:’, $time);

/**
* use the make time function to get the Unix timestamp for the given date
*/

$unit_timestamp = mktime($hour, $minutes, $seconds, $month, $day, $year);

return $unit_timestamp;
}

//convert timestamp to ago format
function convertToAgoFormat($timestamp){
date_default_timezone_set(‘Asia/Kuala_Lumpur’);
$diffBtwCurrentTimeAndTimestamp = time() – $timestamp;
$periodsString = ["sec", "min", "hr", "day", "week", "month", "year", "decade"];
$periodsNumber = ["60", "60", "24", "7", "4.35", "12", "10"];

for($iterator = 0; $diffBtwCurrentTimeAndTimestamp >= $periodsNumber[$iterator]; $iterator++)
$diffBtwCurrentTimeAndTimestamp /= $periodsNumber[$iterator];
$diffBtwCurrentTimeAndTimestamp = round($diffBtwCurrentTimeAndTimestamp);

if($diffBtwCurrentTimeAndTimestamp != 1) $periodsString[$iterator].="s";
$output = "$diffBtwCurrentTimeAndTimestamp $periodsString[$iterator]"; //2 days

return "Posted ".$output." ago";
}

<strong>//Call the function</strong>

$unixTimestamp = convertToUnixTimestamp($posted_at);

<div style="width: 350px;">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. <br>;
<span style="float: right; font-size: 10px; color: gray;">;
<?php echo convertToAgoFormat($unixTimestamp); ?>
</span>;
</div>;
?>
[/php]

Output:


timeago
Convert MySQL Timestamp to Ago Time Format in PHP
Tagged on:         

Leave a Reply

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