Christian Kolb
Christian Kolb

Reputation: 1388

Symfony Doctrine query entries by datetime and timezone

I've got an webservice written with Symfony. An app calls to it and enters messages which have to be send to the app at a specific time. A cronjob on the server runs every minute and checks which messages have to be send. But the user can define a time frame when he whants to receive those messages.

I query those messages the following way:

$query = $em->createQuery('
    SELECT m
    FROM MyBundle:Message m
    INNER JOIN m.user u
    WHERE m.active = true
        AND m.nextMessageOn <= :now
        AND u.receiveMessagesFrom <= :now
        AND u.receiveMessagesTo >= :now
    ORDER BY m.id ASC
')->setParameter('now', new \DateTime('now'));

(For presentation purpose the "from time" has to be before the "to time")

Now I've got the problem, that the user can have a different timezone than the server.

Is there some kind of best practice how to handle the hole time (datetime) / timezone / server / app problem? And especially for Symfony (Doctrine) is there a clean way to query those items?

Upvotes: 0

Views: 4814

Answers (1)

Pi Wi
Pi Wi

Reputation: 1085

You should save the correct timezone for the user in the database and read this: http://docs.doctrine-project.org/en/latest/cookbook/working-with-datetime.html

Upvotes: 3

Related Questions