2010-07-14 9 views
10

ho appuntamento con: {$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'}smarty e data

Ma come arrivare 20 giorni dopo?

Se ora: 2010 05 05 12:12:12, desidero mostrare 2010 25 05 12:12:12

risposta

6

utilizzare la funzione php strtotime() e assegnare la variabile a Smarty. Qualcosa di simile a questo:

<?php 
$later = strtotime('+20 day'); 
$smarty->assign('later', $later); 
?> 

Poi nel modello:

{ $later|date_format:'%Y-%m-%d %H:%M:%S'} 
+0

Grazie, ma non posso utilizzare il codice PHP. Forse è la decisione con solo Smarty? – lolalola

+0

Ora ci sono altre risposte che si basano interamente su smarty e non richiedono il codice PHP – skrilled

18

{$smarty.now} è un semplice timestamp (numero di secondi dal 1970). Quindi, si può semplicemente aggiungere il numero di secondi per come è necessario:

{$smarty.now+20*24*60*60|date_format:'%Y-%m-%d %H:%M:%S'} //+20 days 

Questo funziona in Smarty3, se non nelle versioni precedenti, allora potrebbe essere necessario fare i conti con {assign} e/o {math} direttive.

+0

Funziona anche su Smarty 2 Posso confermare. – crmpicco

3

È possibile utilizzare strtotime() direttamente come modificatore.

{"+20 days"|strtotime|date_format:"Y/m/d"} 
1

Nelle versioni più recenti di Smarty si strtotime qualsiasi stringa precedi

Vale a dire invece di fare {$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'} si può anche fare {"now"|date_format:'%Y-%m-%d %H:%M:%S'}

per ottenere la data di 20 giorni da oggi, si può fare:

{"+20 days"|date_format:"%Y-%m-%d"}

-1

testato in Smarty: Aggiungere 1 giorno, 2 giorni ..... 365 giorni in data dinamica.

$one= date("Y-m-d", strtotime(date("Y-m-d", strtotime('$add dynamic date variable')) . " + 1 day")); 
    $this->smarty->assign('one',$one); 

$two= date("Y-m-d", strtotime(date("Y-m-d", strtotime('$add dynamic date variable')) . " + 2 day")); 
    $this->smarty->assign('two',$two); 
... 
.. 

$oneyear= date("Y-m-d", strtotime(date("Y-m-d", strtotime('$add dynamic date variable')) . " + 365 day")); 
    $this->smarty->assign('oneyear',$oneyear); 
0
{assign var="iItemOne" value=$smarty.now} 
{assign var="iItemTwo" value=1296000} //60*60*24*15-> for 15 days 
{assign var="iSum" value=$iItemOne+$iItemTwo} 

{$iSum|date_format:'%Y-%m-%d %H:%M:%S'}