Twig框架和日期时间错误

1 php mysql frameworks twig

我在Apache 2.28上运行一个由PHP/MySQL驱动的事件站点.我可以根据http://devzone.zend.com/article/13633显示HTML表格.

对于localhost上的这个站点,我正在使用www中提到的Twig框架.树枝项目.组织

内容从本地MySQL数据库中提取:

我的代码:

    <html>
  <head>
    <style type="text/css">
      table {
        border-collapse: collapse;
      }        
      tr.heading {      
        font-weight: bolder;
      }        
      td {
        border: 1px solid black;
        padding: 0 0.5em;
      }    
    </style>  
  </head>
  <body>
    <h2>Events</h2>
    <table>
      <tr class="heading">
        <td>Event time</td>
        <td>Event name</td>
      </tr> 
      {% for d in data %}
      <tr>
        <td>{{ d.evtime|escape }}</td>
        <td>{{ d.evname|escape }}</td>
      </tr> 
      {% endfor %}
    </table>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

// PHP文件如下

    <?php
// include and register Twig auto-loader
include 'Twig/Autoloader.php';
Twig_Autoloader::register();

// attempt a connection
try {
  $dbh = new PDO('mysql:dbname=world;host=localhost', 'root', 'MYPASS');
} catch (PDOException $e) {
  echo "Error: Could not connect. " . $e->getMessage();
}

// set error mode
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// attempt some queries
try {
  // execute SELECT query
  // store each row as an object
  $sql = "SELECT * FROM myeventdb";
  $sth = $dbh->query($sql);
  while ($row = $sth->fetchObject()) {
    $data[] = $row;
  }

  // close connection, clean up
  unset($dbh); 

  // define template directory location
  $loader = new Twig_Loader_Filesystem('templates');

  // initialize Twig environment
  $twig = new Twig_Environment($loader);

  // load template
  $template = $twig->loadTemplate('countries.tmpl');

  // set template variables
  // render template
  echo $template->render(array (
    'data' => $data
  ));

} catch (Exception $e) {
  die ('ERROR: ' . $e->getMessage());
}
?>
Run Code Online (Sandbox Code Playgroud)

但是,我无法将日期时间显示为我的活动:1:30 pm Geography Class

相反,它显示为13:30:00地理类

在Twig语法中,为什么这个以及我需要修复它?我对此很新,我查看了文档,但网站上没有太多关于它的内容.

干杯.

Aus*_*yde 6

因此脚本显示13:30:00,因为这是数据库中的内容 - 您没有在任何地方格式化日期.

在Twig模板中,您可以根据PHP 函数格式使用date过滤器根据自己的喜好格式化日期:date

{{ d.evtime|date('g:ia')|escape }}
Run Code Online (Sandbox Code Playgroud)

如果你想提前做的格式,只需使用的组合datestrtotime:

$formatted_time = date('g:ia',strtotime($unformatted_time));
Run Code Online (Sandbox Code Playgroud)