2016-04-18 43 views
0

我想用phalcon創建一個博客存檔。但我無法完美地理解它。在我的博客表中,我有一個字段datetime,就像「2016-04-15 23:08:40」這種格式。我無法分開年份,月份,日期和時間。我如何序列化它?或如何進行查詢?我現在只是一個簡單的查詢。Phalcon的博客存檔

[控制器]

public function indexAction() 
{ 
    $bloger = Blogs::find(); 
    $this->view->setVar('counts', count($bloger)); 
    $this->view->setVar('blogs', $bloger); 

$archive = Blogs::find(["order" => "datetime DESC"]); 
    $this->view->setVar('archives',$archive); 
} 

[伏]

{% for archive in archives %} 
<a href="blog/showfull/{{archive.id}}">{{ archive.datetime }}</a> 
{% endfor %} 

{% for bloger in blogs %} 
{{bloger.bintro}}<br/> 
{{bloger.bdesc}}<br/> 
{{bloger.bconcl}}<br/> 
{% endfor %} 

存檔正在渲染等: 2016年4月16日12時30分05秒

,但我想要

2016-> 
January-> 
date-1->time 
date-2->time 
February-> 
date-1->time 
date-2->time 
2015-> 
January-> 
date-1->time 
date-2->time 
February-> 
date-1->time 
date-2->time 

如何使這個查詢和如何檢索像這樣?請幫助我一個輕量級的工作示例。

[伏]

我只是嘗試這樣的,但如何分組呢?我的意思是月份和月份下的月份,以及如何設置鏈接?

<?php 
$year = date('Y',strtotime($archive->datetime)); 
$month = date('F',strtotime($archive->datetime)); 
$day = date('d',strtotime($archive->datetime)); 
$time = date('H:i:s',strtotime($archive->datetime)); 

echo('<ul><li>'); 
echo($year.'<ul><li>'); 
echo($month.'<ul><li>'); 
echo($day.' '.$time.'</li></ul></li></ul></li></ul>'); 
?> 

回答

0

您可以像使用PHP一樣在Volt中格式化日期。

{% for archive in archives %} 
<a href="blog/showfull/{{archive.id}}">{{ archive.datetime }}</a> 

Year: {{ date('Y', strtotime(archive.datetime)) }} 
Month: {{ date('F', strtotime(archive.datetime)) }} 
Time: {{ date('H:i:s', strtotime(archive.datetime)) }} 
... 

{% endfor %} 

參考:https://docs.phalconphp.com/en/latest/reference/volt.html#functions

+0

表示 「注意:所遇到的一個非良好地形成數字值」,它可能是這樣的:「<?PHP的日期( 'Y',的strtotime($ archive->日期時間)) ;?>」。但先生它沒有解決我的問題。我需要完整的example.how按年份,然後按月和按日來分組?請 – munaz

+0

我不確定這是否可用默認的伏特函數。但是,如果你想循環使用PHP的日期,那麼看看這個答案:http://stackoverflow.com/questions/3207749/i-have-2-dates-in-php-how-can-i-run -a-foreach-loop-to-go-through-all-of-the-d – Timothy

+0

仍然我無法想象它。請你可以編輯我的代碼?正常工作。 – munaz