-
-
Save elhardoum/91200d0407957d54e9a90279b5775c2e to your computer and use it in GitHub Desktop.
Smarty relative date ... (time) agoSave to plugins folderUsage: <%$date|relative_date%>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Smarty plugin | |
* ------------------------------------------------------------- | |
* Type: modifier | |
* Name: relative_date | |
* Version: 1.1 | |
* Date: November 28, 2008 | |
* Author: Chris Wheeler <[email protected]> | |
* Purpose: Output dates relative to the current time | |
* Input: timestamp = UNIX timestamp or a date which can be converted by strtotime() | |
* days = use date only and ignore the time | |
* format = (optional) a php date format (for dates over 1 year) | |
* ------------------------------------------------------------- | |
*/ | |
if ( !function_exists('smarty_modifier_relative_date') ) { | |
function smarty_modifier_relative_date($timestamp, $days = false, $format = "Y") { | |
if (!is_numeric($timestamp)) { | |
// It's not a time stamp, so try to convert it... | |
$timestamp = strtotime($timestamp); | |
} | |
if (!is_numeric($timestamp)) { | |
// If its still not numeric, the format is not valid | |
return false; | |
} | |
// Calculate the difference in seconds | |
$difference = time() - $timestamp; | |
// Check if we only want to calculate based on the day | |
if ($days && $difference < (60*60*24)) { | |
return "Today"; | |
} | |
if ($difference < 3) { | |
return "Just now"; | |
} | |
if ($difference < 60) { | |
return $difference . " seconds"; | |
} | |
if ($difference < (60*2)) { | |
return "1 minute"; | |
} | |
if ($difference < (60*60)) { | |
return intval($difference / 60) . " minutes"; | |
} | |
if ($difference < (60*60*2)) { | |
return "1 hour"; | |
} | |
if ($difference < (60*60*24)) { | |
return intval($difference / (60*60)) . " hours"; | |
} | |
if ($difference < (60*60*24*2)) { | |
return "Yesterday"; | |
} | |
if ($difference < (60*60*24*7)) { | |
$days = intval($difference / (60*60*24)); | |
return date('l', time()-(86400/*day in sec*/*$days)); | |
} | |
if ($difference < (60*60*24*7*2)) { | |
return "1 week"; | |
} | |
if ($difference < (60*60*24*7*(52/12))) { | |
return intval($difference / (60*60*24*7)) . " weeks"; | |
} | |
if ($difference < (60*60*24*7*(52/12)*2)) { | |
return "1 month"; | |
} | |
if ($difference < (60*60*24*364)) { | |
return intval($difference / (60*60*24*7*(52/12))) . " months"; | |
} | |
// More than a year ago, just return the formatted date | |
return @date($format, $timestamp); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment