如何在Smarty中计算两个日期之间的天数



在这种情况下,我创建了一个员工的数据。我在PHP中使用了一个数组,并分配了变量,并将其称为Smarty,并在PHP的帮助下创建了一个表。在此,我想计算D.O.J和当前日期之间的天数。如何计算D.O.J和当前日期之间的天数。请帮助我。

PHP代码:

<?php
    include_once "../prepengine-header.php";
    $users = array(
        1 => array(
            'id' => '00AC',
            'name' => 'john',
            'address' => 'California',
            'email' => 'JOHn@yAhOO.com',
            'dob' => '1989/10/06',
            'doj' => '2014/12/04'
            ),
        2 => array(
            'id' => '00XV',
            'name' => 'brad',
            'address' => 'Washington',
            'email' => 'bRAd@gmail.com',
            'dob' => '1980/09/23',
            'doj' => '2005/03/10'
           ),
        3 => array(
            'id' => '00UY',
            'name' => 'swati',
            'address' => 'Mutthiganj',
            'email' => 'SWAti@yahoo.com',
            'dob' => '1990/05/04',
            'doj' => '2013/01/02'
            ),
        4 => array(
            'id' => '002VC',
            'name' => 'smith',
            'address' => 'California',
            'email' => 'SMITH@yahoo.com',
            'dob' => '1989/10/22',
            'doj' => '2013/07/15'
            ),
        5 => array(
            'id' => '00RK',
            'name' => 'crystal',
            'address' => 'New York',
            'email' => 'crystal@GMAIL.com',
            'dob' => '1991/05/28',
            'doj' => '2015/01/15'
            ),
        6 => array(
            'id' => '00PC',
            'name' => 'virat',
            'address' => 'Vadodara',
            'email' => 'VIraT@Yahoo.com',
            'dob' => '1989/01/24',
            'doj' => '2013/04/01'
            ),
    );   
    $head[] = "Serial no.";
    $head = array_merge($head, array_keys($users[1]));
    $theme->assign('head',  $head);
    $theme->assign("table", $users);
    echo($theme->fetch('smartart/p_screen5.tpl'));  
?>

智能代码:

    <html>
    <head>
        <title>Screen5</title>
    <style>
    table,tr, th, td, thead
    {
        border: 2px solid #333;
    }
    .rwd-table
    {
        width: 74%;
        height: 77%; 
        text-align: center;   
    } 
    .rwd-table {
        margin: 3em 10em;
        min-width: 300px; 
    } 
    th{
        height: 45px;
        color:  #ADFF2F;
    }  
    body {
      padding: 0 3em;
      font-family: Montserrat, sans-serif;
      color: #444;
      background: #eee;
    }
    .rwd-table {
      background: #34495E;
      color: #fff;
      border-radius: .4em;
      overflow: hidden;
    }
      tr {
        border-color: lighten(#34495E, 10%);
      }
    </style>
    </head>
    <body>
        <form id="data_table" name="data_table">
            <table class="rwd-table">
                <thead>
                    <{foreach from = $head key = heading item = file}>
                        <th><{$file|upper}></th>
                    <{/foreach}>    
                </thead>
                <{foreach from = $table key = heading item = file}>
                <tr>
                    <td><{counter}></td>
                    <td><{$file.id}></td>
                    <td><{$file.name|ucfirst}></td>
                    <td><{$file.address}></td>
                    <td><{$file.email}></td>
                    <td><{$file.dob}></td>
                    <td><{$file.doj}></td>
                </tr>
                <{/foreach}>
            </table>
        </form>
    </body>
   </html>

我的桌子看起来像:https://www.screencast.com/t/ivjk6fp46ef

我从您的代码中了解的是,在日期之间获得日期的简单解决方案是

function getDaysBetweenTwoDates($date1,$date2){
 $date1 = date('y-m-d',strtotime($date1));
 $date2 = date('y-m-d',strtotime($date2));
 $now = new DateTime($date1);
 $ago = new DateTime($date2);
 $diff = $now->diff($ago);
 return $diff->days;
}

只需通过此功能中的两个日期,例如这个

echo getDaysBetweenTwoDates('2017/08/01','2017/07/08'); //24
echo getDaysBetweenTwoDates('2017-08-01','2016-07-08'); //389

在这里您可以看到运行代码

执行以下操作:

PHP代码:

<?php
    include_once "../prepengine-header.php";
    $today = time();
    $users = array(
        1 => array(
            'id' => '00AC',
            'name' => 'john',
            'address' => 'California',
            'email' => 'JOHn@yAhOO.com',
            'dob' => '1989/10/06',
            'doj' => '2014/12/04',
            'diff' => floor(($today - strtotime('2014/12/04')) / 86400)
            ),
        2 => array(
            'id' => '00XV',
            'name' => 'brad',
            'address' => 'Washington',
            'email' => 'bRAd@gmail.com',
            'dob' => '1980/09/23',
            'doj' => '2005/03/10',
            'diff' => floor(($today - strtotime('2005/03/10')) / 86400)
           ),
        3 => array(
            'id' => '00UY',
            'name' => 'swati',
            'address' => 'Mutthiganj',
            'email' => 'SWAti@yahoo.com',
            'dob' => '1990/05/04',
            'doj' => '2013/01/02',
            'diff' => floor(($today - strtotime('2013/01/02')) / 86400)
            ),
        4 => array(
            'id' => '002VC',
            'name' => 'smith',
            'address' => 'California',
            'email' => 'SMITH@yahoo.com',
            'dob' => '1989/10/22',
            'doj' => '2013/07/15',
            'diff' => floor(($today - strtotime('2013/07/15')) / 86400)
            ),
        5 => array(
            'id' => '00RK',
            'name' => 'crystal',
            'address' => 'New York',
            'email' => 'crystal@GMAIL.com',
            'dob' => '1991/05/28',
            'doj' => '2015/01/15',
            'diff' => floor(($today - strtotime('2015/01/15')) / 86400)
            ),
        6 => array(
            'id' => '00PC',
            'name' => 'virat',
            'address' => 'Vadodara',
            'email' => 'VIraT@Yahoo.com',
            'dob' => '1989/01/24',
            'doj' => '2013/04/01',
            'diff' => floor(($today - strtotime('2013/04/01')) / 86400)
            ),
    );   
    $head[] = "Serial no.";
    $head = array_merge($head, array_keys($users[1]));
    $theme->assign('head',  $head);
    $theme->assign("table", $users);
    echo($theme->fetch('smartart/p_screen5.tpl'));  
?>

和您的形式:

<form id="data_table" name="data_table">
            <table class="rwd-table">
                <thead>
                    <{foreach from = $head key = heading item = file}>
                        <th><{$file|upper}></th>
                    <{/foreach}>    
                </thead>
                <{foreach from = $table key = heading item = file}>
                <tr>
                    <td><{counter}></td>
                    <td><{$file.id}></td>
                    <td><{$file.name|ucfirst}></td>
                    <td><{$file.address}></td>
                    <td><{$file.email}></td>
                    <td><{$file.dob}></td>
                    <td><{$file.doj}></td>
                    <td><{$file.diff}></td>
                </tr>
                <{/foreach}>
            </table>
</form>

就是这样。

基本上我所做的是,我只是在每个数组中添加了一个新元素" diff"。并在PHP代码的顶部定义了一个新的变量$today

和形式,我刚刚在表中添加了一个新的" TD",其中包含代码{$file.diff}。它将在您的表格中打印差异。

请尝试一下。

最新更新