date_diff Function
Script Last Updated: Thursday, July 30th, 2009
Documentation Last Updated: Monday, August 10th, 2009
This is a Smarty Function file that can be installed into your plugins for smarty to provide the capability of calculating the time between two dates in either minutes, days, weeks, and years.
If you want to just copy and paste the code instead of downloading it, use this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?php /* * Smarty plugin * ------------------------------------------------------------- * Type: function * Name: date_diff * Version: 2.0 * Date: June 22, 2008 * Author: Matt DeKok * Purpose: factor difference between two dates in days, weeks, * or years * Input: date1 = "mm/dd/yyyy" or "yyyy/mm/dd" or "yyyy-mm-dd" * date2 = "mm/dd/yyyy" or "yyyy/mm/dd" or "yyyy-mm-dd" or $smarty.now * assign = name of variable to assign difference to * interval = "days" (default), "weeks", "years" * Examples: {date_diff date1="5/12/2003" date2=$smarty.now interval="weeks"} * {date_diff date1="5/12/2003" date2="5/10/2008" assign="diff"}{$diff} * ------------------------------------------------------------- */ function smarty_function_date_diff($params, &$smarty) { $date1 = mktime(0,0,0,1,1,2000); $date2 = mktime(0,0,0,date("m"),date("d"),date("Y")); $assign = null; $interval = "days"; extract($params); $i = 1/60/60/24; if($interval == "weeks") { $i = $i/7; } elseif($interval == "years") { $i = $i/365.25; } $date1 = ((is_string($date1))?strtotime($date1):$date1); $date2 = ((is_string($date2))?strtotime($date2):$date2); if($assign != null) { $smarty->assign($assign,floor(($date2 - $date1)*$i)); } else { return floor(($date2 - $date1)*$i); } } ?> |
The following is an example of how to use this function in a smarty template.
1 2 |
{date_diff date1="5/12/2003" date2=$smarty.now interval="weeks"}
{date_diff date1="5/12/2003" date2="5/10/2008" assign="diff"}{$diff} |
280 Unique Visitors