Symfony and Doctrine custom date formatting

DrJohnZoidberg

Honorary Master
Joined
Jul 24, 2006
Messages
28,104
Reaction score
7,605
Location
Table View
I've just started getting into Symfony and I'm busy writing a bundle to query my db tables and return json output.

What I'm trying to do is format my datetime type as a php formatted type and cannot figure out how I should go about this.

At the moment my datetime is being formatted like this:

Code:
datestamp: "2014-08-11T23:50:06+0200"

What I want is something like this:

Code:
datestamp: "2014-08-11 23:50"

Here is the code snippet of my query:

Code:
$query = $repository->createQueryBuilder('p')
                    ->select('p.datestamp','p.users')
                    ->where('p.datestamp BETWEEN :start AND :end')
                    ->setParameter('start', $graph_x_start)
                    ->setParameter('end', $graph_x_end)
                    ->orderBy('p.datestamp', 'DESC')
                    ->getQuery();

                $results = $query->getResult();

                return $this->render('ORSDataFactoryBundle:Default:json.html.twig', array('json_output' => $results));

and my twig file looks like this:

Code:
{{ json_output | serialize }}

I would prefer not having to write a separate function to iterate through everything just to change the date format, I'm thinking there is a simpler way of doing this.

I've been looking into custom mapping types, but I'm a bit out of my depth here.

Anyone with some pointers here?

TIA
 
Okay, I've got around the issue but still not exactly like I wanted.

I'm using a bundle that adds some MYSQL functions to DQL queries, so now I can do this:

Code:
$dql_query = $em->createQuery(
                        'SELECT DATE_FORMAT(p.datestamp,\'%Y-%m-%d %H:%i\') as datestamp, p.users
                         FROM '.$name.' p
                         WHERE p.datestamp BETWEEN :start AND :end
                         ORDER BY p.datestamp'
                        )->setParameter('start',$graph_x_start)
                        ->setParameter('end', $graph_x_end);

I'd still prefer to use the QueryBuilder though.

Still open to suggestions, but at least it's working in the meantime.
 
Code:
datestamp: "2014-08-11T23:50:06+0200"

What I want is something like this:

Code:
datestamp: "2014-08-11 23:50"

variable->format('yyyy-MM-dd HH:mm')

Could try that.

You'll need to use the setParameter function for that
 
Could try that.

You'll need to use the setParameter function for that

Perhaps set the twig environment date format?

Code:
$twig = new Twig_Environment($loader);
$twig->getExtension('core')->setDateFormat('d/m/Y', '%d days');

Taken from http://twig.sensiolabs.org/doc/filters/date.html

Set the format to include your time part as needed.

Thanks for the pointers guys, will try these out when I write the client application.

Decided to do a proper server-client application using REST and will be leaving that datetime notation and then just parse it on the clients end (moment.js can actually do this).

My brain is a bit fried trying to learn how to write a fully RESTful service, but getting there!

I see myself asking a lot more questions in the coming weeks!: D
 
Top
Sign up to the MyBroadband newsletter
X