【问题标题】:print mysql date using smarty使用 smarty 打印 mysql 日期
【发布时间】:2012-08-04 14:29:24
【问题描述】:

我使用这种方法2012-08-02 02:20:05 将日期放入mysql 数据库中。现在,如何使用 smarty 在我的页面中打印。

mycode :(注意:send_date 是行的名称)

$sql = "SELECT m.*, s.photo, s.gender
       FROM mail AS m, signup AS s
       WHERE m.receiver = '" .mysql_real_escape_string($username). "'
       AND m.sender = s.username AND inbox = '1' AND status = '1'
       ORDER BY send_date DESC LIMIT " .$limit;
$rs  = $conn->execute($sql);

【问题讨论】:

    标签: php mysql date smarty


    【解决方案1】:

    要将日期从 2012-08-02 02:20:05 转换为 unix 纪元(自 1970 年以来的秒数),您需要使用 strtotime()。要将纪元格式化为任何人类可读的日期,请使用 date()strtotime()

    Smarty 知道date_format modifier。由于 Smarty3,这只小狗几乎接受任何代表日期的东西(包括2012-08-02 02:20:05)。在 Smarty2 中,您可能需要自己将字符串转换为纪元,例如 {$date|strtotime|date_forma:"%d.$m.%Y"}。请注意,由于 Smarty3 date_format 接受来自 strtotime 和 date 的格式符号,而 Smarty2 仅支持 strtotime 符号。

    【讨论】:

      【解决方案2】:

      如果您询问如何在 smarty 中输出任何变量,请执行以下操作(假设您的 smarty 设置正确):

      在 PHP 中:

      /*
      assign method: assigns variables to the template
      first parameter: The variable name you want to use in your template file
      second parameter: the parameter you want to pass to your template file
      */
      $smarty->assign('variable', $variable);
      

      聪明的:

      /*
      brackets {} indicate that smarty is in play
      $variable is the variable you passed to smarty
      */
      
      {$variable}
      

      如果这不是您想要的,请编辑您的问题以使其更清楚。

      【讨论】:

      • 我知道!如何创建$变量?
      • $variable 是我假设您从数据库中检索的值。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-28
      • 2013-05-27
      • 1970-01-01
      • 1970-01-01
      • 2017-04-24
      相关资源
      最近更新 更多