[TYPO3-mvc] How to compare a DateTime object correctly in an Extbase query?

Stephan Schuler Stephan.Schuler at netlogix.de
Mon Jan 12 15:08:23 CET 2015


Hey there.

Did you dig into extbase query building? What does the created MySQL query look like and what's the difference to the query you expect?

First: Using DateTime objects directly should be fine.
Did you actually try just skipping the ->format() completely?

Second: Time zones are always messy.
Unix timestamps (there are rumours this can be changed but I never did) are UTC by definition.
In contrast to that, "new \DateTime()" gives you the very time zone defined in php.ini or the servers default time zone if no configuration is set. So there are chances this is "GMT+1" on your server.

You could either cast your DateTime object to UTC or return the unix time stamp ("U") to compare.
If you decide for making the DateTime UTC, keep in mind this changes the nested property of your model. So if you pass that $post to e.g. fluid later or hand it to an repository::update(), this could change database contents on every click.

Regards,


Stephan Schuler
Web-Entwickler | netlogix Media

Telefon: +49 (911) 539909 - 0
E-Mail: Stephan.Schuler at netlogix.de
Web: media.netlogix.de




netlogix GmbH & Co. KG
IT-Services | IT-Training | Media
Neuwieder Straße 10 | 90411 Nürnberg
Telefon: +49 (911) 539909 - 0 | Fax: +49 (911) 539909 - 99
E-Mail: info at netlogix.de | Web: http://www.netlogix.de

netlogix GmbH & Co. KG ist eingetragen am Amtsgericht Nürnberg (HRA 13338)
Persönlich haftende Gesellschafterin: netlogix Verwaltungs GmbH (HRB 20634)
Umsatzsteuer-Identifikationsnummer: DE 233472254
Geschäftsführer: Stefan Buchta, Matthias Schmidt



-----Ursprüngliche Nachricht-----
Von: typo3-project-typo3v4mvc-bounces at lists.typo3.org [mailto:typo3-project-typo3v4mvc-bounces at lists.typo3.org] Im Auftrag von Jan Kornblum
Gesendet: Montag, 12. Januar 2015 10:01
An: typo3-project-typo3v4mvc at lists.typo3.org
Betreff: Re: [TYPO3-mvc] How to compare a DateTime object correctly in an Extbase query?

Hello again,

This really is a strange behaviour but i can't find my mistake. The code below works for 95% of my records. But there are some records where it doesn't work (although all have a valid DateTime value)! In this case the "findNext()" always returns the same object as the object passed as a parameter.

Kind regards, Jan

> i've to compare a DateTime property in a repository query to implement
> a "previous" / "next" navigation for a single view. What should the
> query look like correctly? This is what i've done so far:
>
> public function findPrevious(\Vendor\Ext\Domain\Model\Post $post) {
> $query = $this->createQuery();
> $query->setOrderings(array('datePublished' =>
> \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING));
> $query->matching($query->greaterThan('datePublished',
> $post->getDatePublished()->format('Y-m-d H:i:s')));
> $previousPost = $query->execute()->getFirst();
> return $previousPost;
> }
>
> public function findNext(\Vendor\Ext\Domain\Model\Post $post) {
> $query = $this->createQuery();
> $query->setOrderings(array('datePublished' =>
> \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING));
> $query->matching($query->lessThan('datePublished',
> $post->getDatePublished()->format('Y-m-d 00:00:00')));
> $nextPost = $query->execute()->getFirst();
> return $nextPost;
> }
>
> But it only works when i use "->format('Y-m-d 00:00:00')" on the
> DateTime object in the second query. Which "format" must be used to
> get an exact comparison?


_______________________________________________
TYPO3-project-typo3v4mvc mailing list
TYPO3-project-typo3v4mvc at lists.typo3.org
http://lists.typo3.org/cgi-bin/mailman/listinfo/typo3-project-typo3v4mvc


More information about the TYPO3-project-typo3v4mvc mailing list