<pre><code>Entry::find()-&gt;startDate('&gt;='. $someTime).endTime('&lt;='.$someOtherTime).all();
</code></pre>

<p>Those variables can be timestamps, DateTimes or a correctly formated time string.</p>

<p>Here are some variants of these filters</p>

<pre><code>Entry::find()
    -&gt;variantOne('&lt;=' .  Db::prepareDateForDb(new \DateTime())) &lt;-- DateTime object
    -&gt;variantTwo('&lt;=' . time())  &lt;-- as timestamp
    -&gt;variantThree('&lt;=' . (new \DateTime())-&gt;format('U')) &lt;-- timestamp as well
    -&gt;variant(['and', '&lt;=' . time() , '&gt;=' . time() - 3600]) &lt;-- entries within the last hour
    -&gt;validString(['or', '&gt;=' . (new DateTime())-&gt;format('Y-m-d H:i:s'), '&lt;= ' . time()])
</code></pre>

CC BY-SA 3.0
2018-04-19T22:26:03.147Z
I tried `$entites = Entry::find()->startDate('>='. (new \DateTime('now'))->format('m/d/Y') )->all();`, but got an database error like `SQLSTATE[22007]: Invalid datetime format: 7 ERROR: invalid input syntax for type timestamp: ""`
yu xia
CC BY-SA 3.0
2018-04-19T22:49:19.120Z
@yuxia I've added a few examples
Robin Schambach
CC BY-SA 3.0
2018-04-19T23:02:45.437Z
Works just like magic!!! Many many thanks:)
yu xia
CC BY-SA 3.0
2018-05-01T14:00:15.323Z
I had the same problem the other day and solved it like this
`{% set params = {
  section : 'theSection',
  startDate : '>=' ~ someTime,
  endDate : '<=' ~ someOtherTime,
} %}
{% set entries = craft.entries(params).all() %}`
Are there benefits to `Entry::find()` over `craft.entries(params)`?
Johann Dyck
CC BY-SA 3.0
2018-05-01T14:02:04.883Z
@JohannDyck `Entry::find` is PHP Syntax, `craft.entries` is twig syntax that executes `Entry::find()` You can see what `craft.entries` does here https://github.com/craftcms/cms/blob/develop/src/web/twig/variables/CraftVariable.php#L262
Robin Schambach
CC BY-SA 3.0
2018-04-19T21:47:08.063Z
2018-04-19T22:55:09.110Z
2018-04-19T22:55:09.110Z
Robin Schambach
<p>I'm using Craft 3 and I would like to do the following thing:</p>

<p>There is a start date and an end date filed in entry, is it possible to get entries that are between these two dates in PHP?</p>

<p>Thank you! </p>

<datetime>
How to get entries between a time
# How to get entries between a time

(Asked by: username_0 on 2018-04-19)

I'm using Craft 3 and I would like to do the following thing:

There is a start date and an end date filed in entry, is it possible to get entries that are between these two dates in PHP?

Thank you!

**Tags:** datetime


---

## 1 answers

### Answer by username_1 on 2018-04-19. Score: 4 | Accepted answer

Entry::find()->startDate('>='. $someTime).endTime('<='.$someOtherTime).all();

Those variables can be timestamps, DateTimes or a correctly formated time string.
Here are some variants of these filters
Entry::find()
    ->variantOne('<=' .  Db::prepareDateForDb(new \DateTime())) <-- DateTime object
    ->variantTwo('<=' . time())  <-- as timestamp
    ->variantThree('<=' . (new \DateTime())->format('U')) <-- timestamp as well
    ->variant(['and', '<=' . time() , '>=' . time() - 3600]) <-- entries within the last hour
    ->validString(['or', '>=' . (new DateTime())->format('Y-m-d H:i:s'), '<= ' . time()])

> **Comments:**
> **username_0**: I tried `$entites = Entry::find()->startDate('>='. (new \DateTime('now'))->format('m/d/Y') )->all();`, but got an database error like `SQLSTATE[22007]: Invalid datetime format: 7 ERROR: invalid input syntax for type timestamp: ""`  
> **username_1**: @yuxia I've added a few examples  
> **username_0**: Works just like magic!!! Many many thanks:)  
> **username_2**: I had the same problem the other day and solved it like this `{% set params = {   section : 'theSection',   startDate : '>=' ~ someTime,   endDate : '<=' ~ someOtherTime, } %} {% set entries = craft.entries(params).all() %}` Are there benefits to `Entry::find()` over `craft.entries(params)`?  
> **username_1**: @JohannDyck `Entry::find` is PHP Syntax, `craft.entries` is twig syntax that executes `Entry::find()` You can see what `craft.entries` does here https://github.com/craftcms/cms/blob/develop/src/web/twig/variables/CraftVariable.php#L262  

---

