munaz munaz - 7 months ago 12
Javascript Question

Blog archive with Phalcon

I am trying to create a blog archive with phalcon. but i cant figure it perfectly. in my blog table i have a field datetime which is like "2016-04-15 23:08:40" this format. i'm unable to separate year, month, date, and time. and how could i serialize it? or how to make query? i have now just a simple query.

[controller]

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);
}


[volt]

{% 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 %}


Archive is rendering like:
2016-04-16 12:30:05

but i want

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


How to make this query and how to retrieve like this way? please help me with a light-weight working example.

[Volt]

I just try like this but how to grouping it? i mean month under year and day under months, and how set the link?

<?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>');
?>

Answer

You can format dates in Volt the same way as you would in PHP.

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

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

{% endfor %}

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