現在の日時の取得 {$smarty.now}
広告
現在の日時を取得するには {$smarty.now} で参照できます。取得できる値は1970年1月1日から現在までの経過秒数として取得できます。
{$smarty.now}
テンプレートファイル内での記述例は次のようになります。
1970年1月1日から現在までの経過秒数は {$smarty.now} 秒です。
通常は経過秒数のまま使用するのではなく修飾子を用いて年月日の形式に変換して利用します。
現在の日時は {$smarty.now|date_format:'%Y/%m/%d %H:%M:%S'} です。
※修飾子については別のページで詳しく見ていきます。
サンプルプログラム
では簡単なサンプルプログラムを作成して試してみます。
<?php
require_once('Smarty.class.php');
$smarty = new Smarty();
$smarty->template_dir = 'd:/smartysample/reserve/templates/';
$smarty->compile_dir = 'd:/smartysample/reserve/templates_c/';
$smarty->config_dir = 'd:/smartysample/reserve/configs/';
$smarty->cache_dir = 'd:/smartysample/reserve/cache/';
$smarty->assign('title', '予約変数のテスト');
$smarty->display('sample1-1.tpl');
?>
上記を「sample1-1.php」の名前で「(Apacheドキュメントルート)¥smarty¥reserve」に保存します。
{* Smarty reserve/sample1-1.tpl *}
<html>
<head>
<title>Smarty Test</title>
</head>
<body>
<h1>{$title}</h1>
<p>1970年1月1日から現在までの経過秒数は {$smarty.now} 秒です。</p>
<p>現在の日時は {$smarty.now|date_format:'%Y/%m/%d %H:%M:%S'} です。</p>
</body>
</html>
上記を「sample1-1.tpl」の名前で「D:¥smartysample¥reserve¥templates」に保存します。
そしてブラウザから「http://localhost/smarty/reserve/sample1-1.php」へアクセスして下さい。
( Written by Tatsuo Ikura )
PHPBook