title: How to ORDER BY DateTime in Doctrine 2?
body: <p>I'm looking to execute the following query:</p>
  
  <pre><code>        $qb = $this-&gt;getEntityManager()-&gt;createQueryBuilder();
  
          $qb-&gt;select( 'e' )
              -&gt;from( 'Entity\Event',  'e' )
              -&gt;setMaxResults( $limit )
              -&gt;setFirstResult( $offset )
              -&gt;orderBy('e.dateStart', 'ASC');
  
          $events = $qb-&gt;getQuery()-&gt;getResult();
  </code></pre>
  
  <p>Where</p>
  
  <pre><code>/**
   * User
   *
   * @ORM\Table(name="event")
   * @ORM\Entity(repositoryClass="Repositories\EventRepository")
   */
  class Event
  {
      /**
       * @var \DateTime
       *
       * @ORM\Column(name="date_start", type="datetime", precision=0, scale=0, nullable=true, unique=false)
       */
      private $dateStart;
  
  ...
  }
  </code></pre>
  
  <p>But the order by doesn't work.
  My results are not displayed by date start.</p>
  
  <p>I'm looking to retrieve the 20 first events happening from the soonest to the latest</p>
  
  <p>How can I do this ?</p>
  
  <p>Thanks</p>
  
  <p>EDIT:</p>
  
  <p>Following the previous answer, I'm updating my query.
  unfortunatey I still can't have it working.
  Please help</p>
  
  <pre><code>     $qb-&gt;select( 'e' )
          -&gt;from( 'Entity\Event',  'e' )
          -&gt;Where( 
              $qb-&gt;expr()-&gt;andX(
                  $qb-&gt;expr()-&gt;between('e.dateStart', ':from', ':to')
              )
          )
          -&gt;orderBy('e.dateStart', 'ASC')
          -&gt;setFirstResult( $offset )
          -&gt;setMaxResults( $limit );
  </code></pre>
  
  <p>Thanks</p>
  
  <p>EDIT 2:
  It seems moving orderBy did make a difference. I don't have any error as of right now. The script is running fine with orderBy BUT it is NOT ORDERED by datetime at all !</p>
  
  <p>In my results I can't see anything that would make me think it has been ordered based on any given property, definitively not datetime !</p>
  
  <p>How is that possible ?</p>
  
  <p>The Datetime field looks like something like that in the DB:
      2014-05-24 19:30:00</p>
  
  <p>When I var Dump the queries that comes out of the previous query, here is what I have for the datetie field:</p>
  
  <pre><code> ["dateStart"]=&gt; string(8) "DateTime"
  </code></pre>
  
  <p>Does that mean it's really a string for doctrine and that's why it is not sorted by datetime ?</p>
  
  <p>Thanks</p>