2017-09-21 16 views
0

Dans ce scénario, j'ai créé une donnée d'un employé. J'ai pris un tableau en PHP et affecté des variables et les a appelés dans smarty et créé une table avec l'aide de php. En cela, je veux calculer les jours entre D.O.j et la date actuelle. Comment puis-je calculer la différence de jours entre D.O.j et la date actuelle. Veuillez m'aider à cela.comment calculer les jours entre deux dates dans smarty

Code PHP:

<?php 
    include_once "../prepengine-header.php"; 

    $users = array(
     1 => array(
      'id' => '00AC', 
      'name' => 'john', 
      'address' => 'California', 
      'email' => '[email protected]', 
      'dob' => '1989/10/06', 
      'doj' => '2014/12/04' 
      ), 
     2 => array(
      'id' => '00XV', 
      'name' => 'brad', 
      'address' => 'Washington', 
      'email' => '[email protected]', 
      'dob' => '1980/09/23', 
      'doj' => '2005/03/10' 
      ), 
     3 => array(
      'id' => '00UY', 
      'name' => 'swati', 
      'address' => 'Mutthiganj', 
      'email' => '[email protected]', 
      'dob' => '1990/05/04', 
      'doj' => '2013/01/02' 
      ), 
     4 => array(
      'id' => '002VC', 
      'name' => 'smith', 
      'address' => 'California', 
      'email' => '[email protected]', 
      'dob' => '1989/10/22', 
      'doj' => '2013/07/15' 
      ), 
     5 => array(
      'id' => '00RK', 
      'name' => 'crystal', 
      'address' => 'New York', 
      'email' => '[email protected]', 
      'dob' => '1991/05/28', 
      'doj' => '2015/01/15' 
      ), 
     6 => array(
      'id' => '00PC', 
      'name' => 'virat', 
      'address' => 'Vadodara', 
      'email' => '[email protected]', 
      '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')); 
?> 

Code de 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> 

Mon apparence de table comme: https://www.screencast.com/t/IVjk6Fp46Ef

+0

Bienvenue dans Stack Overflow. Qu'avez-vous déjà essayé de faire cela? Veuillez consulter [Combien d'effort de recherche est attendu?] (Https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). Stack Overflow n'est pas un service de codage. Vous êtes censé *** étudier votre problème et faire une bonne tentative pour écrire le code vous-même *** avant de poster. Si vous êtes bloqué sur quelque chose de * spécifique *, revenez en arrière et incluez un [exemple minimal, complet et vérifiable] (https://stackoverflow.com/help/mcve) et un résumé de ce que vous avez essayé, afin que nous puissions vous aider. – FluffyKitten

Répondre

1

ce que je suis comprends de votre code, la solution simple pour obtenir les jours entre les dates sont

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

juste passer les deux dates dans cette fonction, comme celui-ci

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

+0

une idée sur le smarty comment puis-je le définir dans smarty page – punk

+0

Mettez la méthode ci-dessus en dehors de la boucle entre accolades et appelez la méthode dans la boucle en créant une variable et utilisez cette variable. Faites-moi savoir si vous rencontrez un problème. –

+0

Je ne suis pas en mesure d'imprimer dans le tableau – punk

0

Effectuez les opérations suivantes:

Code PHP:

<?php 
    include_once "../prepengine-header.php"; 
    $today = time(); 
    $users = array(
     1 => array(
      'id' => '00AC', 
      'name' => 'john', 
      'address' => 'California', 
      'email' => '[email protected]', 
      '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' => '[email protected]', 
      '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' => '[email protected]', 
      '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' => '[email protected]', 
      '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' => '[email protected]', 
      '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' => '[email protected]', 
      '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')); 
?> 

et dans votre formulaire:

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

C'est tout.

Fondamentalement ce que j'ai fait, j'ai juste ajouté un nouvel élément "diff" dans chacun du tableau. et défini une nouvelle variable $today en haut du code Php.

Et dans la forme je viens d'ajouter un nouveau "td" dans la table qui contient le code {$file.diff}. Il imprimera la différence dans votre formulaire.

Faites un essai.

+0

votre suggestion de travail. – punk

+0

c'est génial, si cela aide vraiment alors s'il vous plaît accepter cela comme une réponse et aussi faire upvote :) –

+0

je suis nouveau dans sof mon repo est inférieur à 15 que je ne peux pas upvote. – punk