0

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>');
?>
munaz
  • 166
  • 2
  • 14

1 Answers1

0

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', strtotime(archive.datetime)) }}
 Month: {{ date('F', strtotime(archive.datetime)) }}
 Time: {{ date('H:i:s', strtotime(archive.datetime)) }}
 ...

{% endfor %}

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

Timothy
  • 2,004
  • 3
  • 23
  • 29
  • showing "Notice: A non well formed numeric value encountered" it may be like this: "datetime));?>". but sir its not solve my problem. i need full example.how to grouping it by year then month then day? please – munaz Apr 19 '16 at 06:38
  • I am not sure if this is possible with the default Volt functions. But if you'd like to loop through dates with PHP, then look at this answer: http://stackoverflow.com/questions/3207749/i-have-2-dates-in-php-how-can-i-run-a-foreach-loop-to-go-through-all-of-those-d – Timothy Apr 19 '16 at 07:24
  • still i'm unable to figure it. please can you edit my code? for working properly. – munaz Apr 19 '16 at 11:49
  • 1
    If you're using some other version of phalcon, let's simple add in compiler of view at bootstrap simple function $compiler->addFunction('strtotime','strtotime'); this will enable you function strtotime in template and solve your problem when you getting some error in example of Timothy. – Kamil Apr 19 '16 at 19:08
  • Is it possible to use [codeigniter](https://en.wikipedia.org/wiki/CodeIgniter) calendar class as a blog archive in [phalcon](https://en.wikipedia.org/wiki/Phalcon_%28framework%29)? – munaz May 21 '16 at 11:53