Reputation: 343
I have two dates in respective format (02-Dec-14 and 17-Dec-14 ) and i want to compare these two dates in smarty.
How can i compare these two dates ? please help.
Thanks In Advance
Upvotes: 2
Views: 11046
Reputation: 649
Here is my smarty function (add to your smarty plugin directory):
/*
* Smarty plugin
* -------------------------------------------------------------
* Type: function
* Name: date_diff
* Author: Rafał Pawlukiewicz
* Purpose: factor difference between two dates in days, weeks, or years
* Input: d1 = "mm/dd/yyyy" or "yyyy/mm/dd" or "yyyy-mm-dd"
* d2 = "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 d1="2020-01-20"}
* Examples: {date_diff d1="2020-01-20" d2=2020-02-10 interval="weeks"}
* Examples: {date_diff d1="2020-01-20" d2=2020-02-10 assign="variable_diff"} result: {$variable_diff}
* -------------------------------------------------------------
*/
function smarty_function_date_diff($params, &$smarty)
{
$d1 = isset($params['d1']) ? $params['d1'] : date('Y-m-d');
$d2 = isset($params['d2']) ? $params['d2'] : date('Y-m-d');
$assign_name = isset($params['assign']) ? $params['assign'] : '';
$date1 = strtotime($d1);
$date2 = strtotime($d2);
// use current for empty string
if (! $date1) {
$date1 = date('Y-m-d');
}
if (! $date2) {
$date2 = date('Y-m-d');
}
$interval = isset($params['interval']) ? $params['interval'] : 'days';
// diff in days
$diff = ($date2 - $date1) / 60 / 60 / 24;
if ($interval === "weeks") {
$diff /= 7;
}
elseif ($interval === "years") {
$diff /= 365.25;
}
$diff = floor($diff);
if ($assign_name) {
$smarty->assign($assign_name, $diff);
}
else {
return $diff;
}
}
Upvotes: 0
Reputation: 3998
I Tried many ways, could not get the solution. finally I tried something like this: In PHP passing 2 variables something like this to smarty:
$insuranceStartDate= date("jS F Y", strtotime($startdate));
$smarty->assign('insuranceStartDate' , $insuranceStartDate);
$insuranceStartDatePlain= date("Y/m/d", strtotime($startdate));
$smarty->assign('insuranceStartDatePlain' , $insuranceStartDatePlain);
In smarty comparing like below:
{if $insuranceStartDatePlain|date_format:'%Y/%m/%d' < '2017/09/01'}
less than - 1<sup>st</sup> Septemeber 2017
{else}
greater - {$insuranceStartDate}
{/if}
Note:to compare dates when they are strings, use order YYYY/MM/DD in smarty
Hope it helps someone in future :)
Upvotes: 0
Reputation: 111869
If you have those dates assigned to Smarty for example this way:
$smarty->assign('date2','02-Dec-14');
$smarty->assign('date1','17-Dec-14');
you can use strtotime
function directly in Smarty, for example:
{if $date1|strtotime < $date2|strtotime}
{$date1} is earlier than {$date2}
{elseif $date1|strtotime == $date2|strtotime}
Both dates are the same
{else}
{$date2} is earlier than {$date1}
{/if}
Upvotes: 6
Reputation: 23958
<?php
$d = '02-Dec-14';
$e = '17-Dec-14';
$t1 = strtotime($d);
$t2 = strtotime($e);
/*
// Test if it works.
if ($t2 > $t1) {
echo 'second is greater';
}
*/
You can then assign it to Smarty:
$this->assign('date_one', $t1);
$this->assign('date_two', $t2);
And in Smarty, you can compare:
{if $date_one < $date_rwo}
...do something..
{/if}
Upvotes: 0