Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Make a pretty date difference from the current time
<?php
/*
Make a pretty date difference from the current time
Author: Luca Zorzi (@LucaTNT)
License: BSD
*/
function parseDate($timestamp)
{
// Current timestamp
$now = time();
// Difference from given timestamp
$difference = $now - $timestamp;
// If less than one hour (59 minutes and 30 seconds, to be precise), we count minutes
if($difference < 3570)
{
$output = round($difference / 60).'m';
}
// If less than 23 hours 59 minutes and 30 seconds, we count hours
elseif ($difference < 86370)
{
$output = round($difference / 3600).'h';
}
// If less than 6 days 23 hours 59 minutes and 30 seconds, we count days
elseif ($difference < 604770)
{
$output = round($difference / 86400).'d';
}
// If less than 164 days 23 hours 59 minutes and 30 seconds, we count weeks
elseif ($difference < 31535970)
{
$output = round($difference / 604770).'w';
}
// Else we count years
else
{
$output = round($difference / 31536000).'y';
}
return $output;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.