2013-10-28 31 views
0

我發佈一條消息,直到某個日期,所以我創建了一個時間變量作爲Smarty的日期上使用TPL文件

$announcedate = strtotime('+6 day'); 
$now = "1382960040"; 
$smarty->assign('announcedate', $announcedate); 
$smarty->assign('now', $now); 

而且包括在第三方物流文件

{if $now > $announcedate}My Announcement{/if} 

我想請務必在使用之前確認並在第6天后特別結束通知。

這是正確的方法嗎?還是有其他的指導?

回答

1

簡而言之:這將按照你的意圖工作。

不過,我想補充幾點建議:

​​3210返回一個整數,你用字符串進行比較。這不一定會導致您的用例出現問題,但無論如何都應該改進。像這樣的簡單更新會改變:

$announcedate = strtotime('+6 day'); 
$now = strtotime('now'); 

您的解決方案適用於您的特定任務,但不是很一般。如果您想將這些代碼用於其他目的(例如其他公告),您很可能必須修改邏輯。出於這些原因,爲了保持與顯示層smarty相距甚遠的邏輯,我只會在我的php代碼中檢查公告。

這將是這個樣子:

PHP(僞代碼,這個想法是,功能myAnncouncements()獲取你需要的特定時段或任何公告):

$smarty->assign('announcements', myAnncouncements()); 

Smarty的模板:

{if isset($announcements)} 
    {foreach $announcements as $item} 
     // whatever is needed in here.... 
    {/foreach} 
{/if} 
+0

這不按預期工作。我想在第六天結束時宣佈結束。我想我的更大的if語句是不正確的 – sammry

+0

如果你想要一個特定的結束日期,那麼只需要用特定的日期替換你的'$ announceate'(pe'$ announceate = strtotime('2013-11-30 12:00:00' );'或者類似的東西)。 – Bjoern

+0

我試過$ announceate = strtotime('2013-10-30 12:00:00'); $ now = strtotime('now'); $ smarty-> assign('announceate',$ announceate); $ smarty-> assign('now',$ now);在tpl文件中,{if $ now> $ announceate} My Announcement {/ if} .......仍然不能按預期工作。一旦時間結束,它會顯示,而不是在時間內,請幫助 – sammry