Skip to content

Instantly share code, notes, and snippets.

@LucaTNT
Created June 17, 2013 20:49
Show Gist options
  • Save LucaTNT/5800253 to your computer and use it in GitHub Desktop.
Save LucaTNT/5800253 to your computer and use it in GitHub Desktop.
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