<p>Have a look at the Dashboard Widget section in the Craft docs. It’ll walk you through defining your “Start Date” and “End Date” settings, and how to render the settings template, where you can make use of the <code>dateTimeField</code> Twig macros to render the Date and Time fields.</p>

<p>An important step to make your widget save the values properly to the database. You’ll have to convert them <code>DateTime</code> objects to string before save in the <code>prepSettings()</code>, and convert them back in the <code>getSettingsHtml()</code> before rendering the settings template. There’s this convenient <code>DateTime::createFromString()</code> helper that you should proabably want to use.</p>

<p>Another thing that you’ll probably find useful is the <code>mySqlDateTime()</code> method that prepares a <code>DateTime</code> object for use in criteria model parameters.</p>

<p>Have a look at <a href="https://github.com/craftcms/store-hours" rel="nofollow noreferrer">P&amp;T Store Hours plugin</a>, that shows some of these methods in code context.</p>

CC BY-SA 3.0
2016-07-01T09:56:09.587Z
2017-11-10T03:02:02.957Z
2017-11-10T03:02:02.957Z
carlcs
<p>In CP, there is the widget called "Recent Entries". You can pull out entries from your sections, limit a count of entries.</p>

<p>I'm building a plugin that has very similar widget to recent entries widget.</p>

<p><strong>THE GOAL</strong></p>

<p>Filter entries by date range within widget.</p>

<p>I was only able to set a fixed date range within widget's class ...</p>

<pre><code>// To get entries from a specific date range
$criteria-&gt;postDate = array('and', '&gt; 2016-06-10 23:59:59', '&lt; 2016-06-30 00:00:00');
</code></pre>

<plugin-development>
Filter entries by date range in plugin
# Filter entries by date range in plugin

(Asked by: username_0 on 2016-07-01)

In CP, there is the widget called "Recent Entries". You can pull out entries from your sections, limit a count of entries.
I'm building a plugin that has very similar widget to recent entries widget.
THE GOAL
Filter entries by date range within widget.
I was only able to set a fixed date range within widget's class ...
// To get entries from a specific date range
$criteria->postDate = array('and', '> 2016-06-10 23:59:59', '< 2016-06-30 00:00:00');

**Tags:** plugin-development


---

## 1 answers

### Answer by username_1 on 2016-07-01. Score: 3 | Accepted answer

Have a look at the Dashboard Widget section in the Craft docs. It’ll walk you through defining your “Start Date” and “End Date” settings, and how to render the settings template, where you can make use of the `dateTimeField` Twig macros to render the Date and Time fields.

An important step to make your widget save the values properly to the database. You’ll have to convert them `DateTime` objects to string before save in the `prepSettings()`, and convert them back in the `getSettingsHtml()` before rendering the settings template. There’s this convenient `DateTime::createFromString()` helper that you should proabably want to use.

Another thing that you’ll probably find useful is the `mySqlDateTime()` method that prepares a `DateTime` object for use in criteria model parameters.

Have a look at [P&T Store Hours plugin](https://github.com/craftcms/store-hours), that shows some of these methods in code context.


---

