|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
sporkfoo Smarty n00b
Joined: 07 Sep 2004 Posts: 1 Location: Raleigh, NC
|
Posted: Tue Sep 07, 2004 9:22 pm Post subject: PLUGIN: date_diff |
|
|
Not sure if anyone else has created this or not, I hope not. But I had some issues where I need the difference in time between two dates and this just seemed like an easy way to handle it. If anyone can see any way to improve it, please feel free to let me know. It works, its simple and its suited me just fine.
[php:1:b7805ab539]<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* Type: function
* Name: date_diff
* Version: 1.0
* Date: March 30, 2003
* Author: Chris Noble <chris@eatatsporks.com>
* Credits Chrome <webmaster@lotekk.net> created a lot of the functionality
* this is based on
* Purpose: factor date and time difference between two dates
* Input: date1 = yyyy-mm-dd
* date2 = yyyy-mm-dd
* display_days = display days
* display_months = display months
* display_years = display years
* Examples: {date_diff date1=2003-5-12 date2=2001-5-12}
* {date_diff date1=2003/5/12 date2=2001/5/12}
* {date_diff date1=2003.5.12 date2=2001.5.12}
* {date_diff date1=2003-5-12 date2=2001-5-12 display_days=false}
* -------------------------------------------------------------
*/
function smarty_function_date_diff($params, &$smarty) {
require_once $smarty->_get_plugin_filepath('shared','make_timestamp');
$display_years = true;
$display_months = true;
$display_days = true;
$year_format = "%Y";
$month_format = "%m";
$day_format = "%d";
$years_text = "year(s)";
$months_text = "month(s)";
$days_text = "day(s)";
$time = time();
extract($params);
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $date1)) {
$date1 = strftime('%Y-%m-%d', smarty_make_timestamp($time));
}
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $date2)) {
$date2 = strftime('%Y-%m-%d', smarty_make_timestamp($time));
}
list($year1, $month1, $day1) = split('[/.-]', $date1);
list($year2, $month2, $day2) = split('[/.-]', $date2);
// configure the base date here
$base_day = $day1;
$base_mon = $month1;
$base_yr = $year1;
// date difference
$diff_day = $day2;
$diff_mon = $month2;
$diff_yr = $year2;
// overflow is always caused by max days of $base_mon
// so we need to know how many days $base_mon had
$base_mon_max = date ("t",mktime (0,0,0,$base_mon,$base_day,$base_yr));
// days left till the end of that month
$base_day_diff = $base_mon_max - $base_day;
// month left till end of that year
// substract one to handle overflow correctly
$base_mon_diff = 12 - $base_mon - 1;
// start on jan 1st of the next year
$start_day = 1;
$start_mon = 1;
$start_yr = $base_yr + 1;
// difference to that 1st of jan
$day_diff = ($diff_day - $start_day) + 1; // add today
$mon_diff = ($diff_mon - $start_mon) + 1; // add current month
$yr_diff = ($diff_yr - $start_yr);
// and add the rest of $base_yr
$day_diff = $day_diff + $base_day_diff;
$mon_diff = $mon_diff + $base_mon_diff;
// handle overflow of days
if ($day_diff >= $base_mon_max)
{
$day_diff = $day_diff - $base_mon_max;
$mon_diff = $mon_diff + 1;
}
// handle overflow of years
if ($mon_diff >= 12)
{
$mon_diff = $mon_diff - 12;
$yr_diff = $yr_diff + 1;
}
if($display_years)
$diff .= $yr_diff . " " . $years_text . " ";
if($display_months)
$diff .= $mon_diff . " " . $months_text . " ";
if($display_days)
$diff .= $day_diff . " " . $days_text . " ";
return $diff;
}
?>[/php:1:b7805ab539] |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|