如何在smarty

时间:2017-09-21 07:02:49

标签: php 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'));  
?>

Smarty代码:

    <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

2 个答案:

答案 0 :(得分:1)

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

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

here you can see the running code

答案 1 :(得分:0)

执行以下操作:

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>

那就是它。

基本上我所做的是,我刚刚添加了一个新元素&#34; diff&#34;在每个阵列中。并在Php代码的顶部定义了一个新变量$today

在Form I中我刚添加了一个新的&#34; td&#34;在包含代码{$file.diff}的表中。它将打印您表单中的差异。

请试一试。