Compare dates between datetimes with Doctrine

Related searches

I have a Syfmony2 app with a table which has a date field. This date field is a DateTime type.

I need to get all the entities which the same date as now.

But if I do:

$now = new \DateTime();
$data = $entityRepository->findByDate($now);

I get 0 results because Doctrine is comparing the DateTime object, and I need to compare only the year, month and day, not hours... only de Date object, not the DateTime.

Any Idea? Thanks :D

I see this simple way:

$now = new \DateTime();

$data = $entityRepository->getByDate($now);

then in your repository

public function getByDate(\Datetime $date)
    $from = new \DateTime($date->format("Y-m-d")." 00:00:00");
    $to   = new \DateTime($date->format("Y-m-d")." 23:59:59");

    $qb = $this->createQueryBuilder("e");
        ->andWhere(' BETWEEN :from AND :to')
        ->setParameter('from', $from )
        ->setParameter('to', $to)
    $result = $qb->getQuery()->getResult();

    return $result;

How can I compare a date with current date using Doctrine 2 , There is another way: $query = $em->createQuery('SELECT d FROM test d WHERE d.expDate > :today') ->setParameter('today', new� Given two dates (date1 and date2) and the task is to compare the given dates. Comparing two dates in PHP is simple when both the dates are in the same format but the problem arises when both dates are in a different format. Method 1: If the given dates are in the same format then use a simple comparison operator to compare the dates. Example:

Method in repository

public function getDays(\DateTime $firstDateTime, \DateTime $lastDateTime)
    $qb = $this->getEntityManager()->createQueryBuilder()
        ->from('ProjectBundle:Calendar', 'c')
        ->where(' BETWEEN :firstDate AND :lastDate')
        ->setParameter('firstDate', $firstDateTime)
        ->setParameter('lastDate', $lastDateTime)

    $result = $qb->getQuery()->getResult();

    return $result;

And action

public function calendarAction()
    $currentMonthDateTime = new \DateTime();
    $firstDateTime = $currentMonthDateTime->modify('first day of this month');
    $currentMonthDateTime = new \DateTime();
    $lastDateTime = $currentMonthDateTime->modify('last day of this month');

    $days = $this->getDoctrine()
        ->getDays($firstDateTime, $lastDateTime);

    return ['days' => $days];

Working with DateTime Instances, Doctrine Object Relational Mapper Documentation: Working with DateTime runs on different servers or is moved from one to another server (with different� The Duration Calculator calculates the number of days, months and years between two dates.

There is a difference between the date and datetime types in doctrine.

date: Type that maps a SQL DATETIME to a PHP DateTime object.

datetime: Type that maps a SQL DATETIME/TIMESTAMP to a PHP DateTime object.

Make sure you have set the column type to date instead of datetime.

Alternatively - as a workaround - you could get the day from the original date1 and then search between a same-day date2 -> 00:00:00 and same-day date3 -> 23:59:59 using a custom repository method.

GreaterThan (Symfony Docs), Basic Usage; Comparing Dates; Options This constraint can be used to compare DateTime objects against any date string accepted by the DateTime� Working with DateTime Instances. There are many nitty gritty details when working with PHPs DateTime instances. You have to know their inner workings pretty well not to make mistakes with date handling. This cookbook entry holds several interesting pieces of information on how to work with PHP DateTime instances in Doctrine 2.

Symfony perfect entity, dates and data | by Anton Lytvynov, DateTime, createAt, updateAt and entities data. A lot of If you use Doctrine in your Symfony project you can make your life much easier. Set two dates. DateTime date1 = new DateTime(2018, 7, 15, 08, 15, 20); DateTime date2 = new DateTime(2018, 8, 17, 11, 14, 25); Now, get the difference between two dates.

Comparing dates and times is a very crucial requirement in any programming language. Python has a datetime library which has many inbuilt functions to use date and time. Interestingly date and time can also be compared like mathematical comparison between various numbers.

Module Example Public Sub Main() Dim date1 As Date = #08/01/2009 12:00AM# Dim date2 As Date = #08/01/2009 12:00PM# Dim result As Integer = DateTime.Compare(date1, date2) Dim relationship As String If result < 0 Then relationship = "is earlier than" ElseIf result = 0 Then relationship = "is the same time as" Else relationship = "is later than

  • When you are ready to move to the next coding level, you guys could read this article about value object and time precision.…
  • you can also use SQL DATE() function following the steps in this excellent answer to a similar question: