Размер файла: 5.62Kb
<html> <head> <meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" > <title> Даты </title> <meta name = "generator" content = "DocBook XSL Stylesheets V1.75.1" > <link rel = "home" href = "index.html" title = "Руководство по Smarty" > <link rel = "up" href = "tips.html" title = "Chapter 18. Советы" > <link rel = "prev" href = "tips.passing.vars.html" title = "Присвоение переменной заголовка (title) шаблону-шапке" > <link rel = "next" href = "tips.wap.html" title = "WAP/WML" > </head> <body bgcolor = "white" text = "black" link = "#0000FF" vlink = "#840084" alink = "#0000FF" > <div class = "navheader" > <table width = "100%" summary = "Navigation header" > <tr><th colspan = "3" align = "center" > Даты </th></tr> <tr> <td width = "20%" align = "left" > <a accesskey = "p" href = "tips.passing.vars.html" > Prev </a> </td> <th width = "60%" align = "center" > Chapter 18. Советы </th> <td width = "20%" align = "right" > <a accesskey = "n" href = "tips.wap.html" > Next </a> </td> </tr> </table> <hr> </div> <div class = "sect1" title = "Даты" > <div class = "titlepage" ><div><div><h2 class = "title" style = " clear : both " > <a name = "tips.dates" ></a> Даты </h2></div></div></div> <p> Обычно даты в Smarty всегда передаются как <a class = "ulink" href = "http://php.net/time" target = "_top" > временные метки </a> (англ. timestamp), что позволяет проектировщикам шаблонов использовать <a class = "link" href = "language.modifier.date.format.html" title = "date_format" ><code class = "varname" > date_format </code></a> для полного контроля над форматированием даты и также делает легким сравнение дат там, где это необходимо. </p> <div class = "note" title = "Note" style = " margin - left : 0.5in ; margin - right : 0.5in ; " > <h3 class = "title" > Note </h3> <p> Начиная с версии Smarty 1.4.0, вы можете передавать даты в Smarty в виде меток времени Unix (unix timestamps), mysql, или в любом другом виде, который понимает функция <a class = "ulink" href = "http://php.net/strtotime" target = "_top" > strtotime() </a> . </p> </div> <div class = "example" > <a name = "id2746704" ></a><p class = "title" ><b> Example 18.4. Использование date_format </b></p> <div class = "example-contents" > <pre class = "programlisting" > {$startDate|date_format} </pre> <p> Результат работы: </p> <pre class = "screen" > Jan 4, 2009 </pre> <pre class = "programlisting" > {$startDate|date_format:"%Y/%m/%d"} </pre> <p> Результат работы: </p> <pre class = "screen" > 2009/01/04 </pre> <p> Даты можно ставнивать в шаблонах путем сравнения меток времени следующим образом: </p> <pre class = "programlisting" > {if $date1 < $date2} ... делаем что-то полезное ... {/if} </pre> </div> </div> <br class = "example-break" ><p> Когда <a class = "link" href = "language.function.html.select.date.html" title = "{html_select_date}" > <code class = "varname" > {html_select_date} </code></a> используется в шаблоне, программист наверняка захочет преобразовать данные из формы назад в формат временной метки. Вот функция, которая поможет вам сделать это. </p> <div class = "example" > <a name = "id2746789" ></a><p class = "title" ><b> Example 18.5. Преобразование элементов формы ввода даты назад к временной метке </b></p> <div class = "example-contents" ><pre class = "programlisting" > <?php // Предполагается, что ваши элементы формы названы // startDate_Day, startDate_Month, startDate_Year $startDate = makeTimeStamp($startDate_Year, $startDate_Month, $startDate_Day); function makeTimeStamp($year='', $month='', $day='') { if(empty($year)) { $year = strftime('%Y'); } if(empty($month)) { $month = strftime('%m'); } if(empty($day)) { $day = strftime('%d'); } return mktime(0, 0, 0, $month, $day, $year); } ?> </pre></div> </div> <br class = "example-break" ><p> См. также <a class = "link" href = "language.function.html.select.date.html" title = "{html_select_date}" > <code class = "varname" > {html_select_date} </code></a> , <a class = "link" href = "language.function.html.select.time.html" title = "{html_select_time}" > <code class = "varname" > {html_select_time} </code></a> , <a class = "link" href = "language.modifier.date.format.html" title = "date_format" > <code class = "varname" > date_format </code></a> и <a class = "link" href = "language.variables.smarty.html#language.variables.smarty.now" title = "{$smarty.now}" > <em class = "parameter" ><code> $smarty.now </code></em></a> </p> </div> <div class = "navfooter" > <hr> <table width = "100%" summary = "Navigation footer" > <tr> <td width = "40%" align = "left" > <a accesskey = "p" href = "tips.passing.vars.html" > Prev </a> </td> <td width = "20%" align = "center" ><a accesskey = "u" href = "tips.html" > Up </a></td> <td width = "40%" align = "right" > <a accesskey = "n" href = "tips.wap.html" > Next </a> </td> </tr> <tr> <td width = "40%" align = "left" valign = "top" > Присвоение переменной заголовка (title) шаблону-шапке </td> <td width = "20%" align = "center" ><a accesskey = "h" href = "index.html" > Home </a></td> <td width = "40%" align = "right" valign = "top" > WAP/WML </td> </tr> </table> </div> </body> </html>