Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 比较smarty中的两个日期_Php_Date_Smarty - Fatal编程技术网

Php 比较smarty中的两个日期

Php 比较smarty中的两个日期,php,date,smarty,Php,Date,Smarty,我有两个日期,分别是02-Dec-14和17-Dec-14,我想用smarty比较这两个日期 我如何比较这两个日期?请帮忙 提前谢谢 <?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'; } */ 在Smarty中,您可以比较

我有两个日期,分别是02-Dec-14和17-Dec-14,我想用smarty比较这两个日期

我如何比较这两个日期?请帮忙

提前谢谢

<?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';
}
*/
在Smarty中,您可以比较:

{if $date_one < $date_rwo}
   ...do something..
{/if}
在Smarty中,您可以比较:

{if $date_one < $date_rwo}
   ...do something..
{/if}

例如,如果您将这些日期指定给Smarty:

$smarty->assign('date2','02-Dec-14');
$smarty->assign('date1','17-Dec-14');
您可以在Smarty中直接使用strotime函数,例如:

{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}

例如,如果您将这些日期指定给Smarty:

$smarty->assign('date2','02-Dec-14');
$smarty->assign('date1','17-Dec-14');
您可以在Smarty中直接使用strotime函数,例如:

{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中,向smarty传递2个类似的变量:

$insuranceStartDate= date("jS F Y", strtotime($startdate));
$smarty->assign('insuranceStartDate' , $insuranceStartDate);
$insuranceStartDatePlain= date("Y/m/d", strtotime($startdate));
$smarty->assign('insuranceStartDatePlain' , $insuranceStartDatePlain);
在smarty中,如下所示:

{if $insuranceStartDatePlain|date_format:'%Y/%m/%d' < '2017/09/01'} 
less than -  1<sup>st</sup> Septemeber 2017
{else}
greater  - {$insuranceStartDate}
{/if}
注意:要比较字符串中的日期,请在smarty中使用YYYY/MM/DD顺序


希望对以后的人有所帮助:

我尝试了很多方法,都没有找到解决方案。最后我尝试了这样的方法: 在PHP中,向smarty传递2个类似的变量:

$insuranceStartDate= date("jS F Y", strtotime($startdate));
$smarty->assign('insuranceStartDate' , $insuranceStartDate);
$insuranceStartDatePlain= date("Y/m/d", strtotime($startdate));
$smarty->assign('insuranceStartDatePlain' , $insuranceStartDatePlain);
在smarty中,如下所示:

{if $insuranceStartDatePlain|date_format:'%Y/%m/%d' < '2017/09/01'} 
less than -  1<sup>st</sup> Septemeber 2017
{else}
greater  - {$insuranceStartDate}
{/if}
注意:要比较字符串中的日期,请在smarty中使用YYYY/MM/DD顺序


希望它对将来的人有所帮助:

以下是我的smarty功能添加到smarty插件目录:

/* 
* 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;
    }

}

以下是我的smarty功能添加到smarty插件目录:

/* 
* 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;
    }

}

您自己尝试过什么吗?您自己尝试过什么吗?请注意,这可能需要您更改Smarty的设置,尤其是$php_修饰符设置。@IMSoP True,但仅当您在Smarty中启用安全性时才可以。默认情况下,它没有启用,所以您可能可以使用所有的PHP函数/修饰符是的,只是认为如果有人尝试此操作但不起作用,则值得一提。@IMSoP是的,这是真的。我们不知道OP在这里的代码是什么-可能是一些支持安全的第三方代码Hanks伙计们,我在lib文件夹中做了一个自定义插件,然后把它们调用到模板文件{$smarty。现在{strotime}date_格式:%Y-%m-%d}注意,这可能需要您更改smarty的,特别是$php_修饰符设置。@IMSoP True,但只有在Smarty中启用安全性时。默认情况下,它没有启用,所以您可能可以使用所有的PHP函数/修饰符是的,只是认为如果有人尝试此操作但不起作用,则值得一提。@IMSoP是的,这是真的。我们不知道这里的OP代码是什么-可能是一些支持安全的第三方代码Hanks伙计们,我在lib文件夹中制作了自定义插件,然后将它们调用到模板文件{$smarty.now | strotime | date|u格式:%Y-%m-%d}