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
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}
<?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}
{if {$date1|date_format:"%y%m%d"} lt {$date2|date_format:"%y%m%d"}}
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 :)