21 votes
Accepted

Display entry post date

{{ entry.postDate | date("M d, Y") }} Inside the date filter, you use the date formatting values documented in the PHP date() documentation. Also, see Craft's DateTime documentation, and the Twig ...
  • 7,258
15 votes
Accepted

Advanced query/filter based on date and values

You're actually closer than you might think. All you need to do is add your field handles as parameters. I prefer to use the hash style of declaring parameters but either way will work, I just find ...
  • 2,311
14 votes
Accepted

Get Age from Date of Birth

Craft's Date/Time field extends PHP's DateTime class so (since 5.3.0) you can use the Diff method like so: yourDateTimeField.diff(now).format('%y')
13 votes
Accepted

How do I set up event listing & detail pages with entry post dates that are in the future?

STEP ONE: Create an "Events" section in Craft and uncheck "Entries in this section have their own URLS". STEP TWO: Create your listing page and use ".status('pending')" to have CRAFT return only ...
11 votes
Accepted

Display entries if Field Date is past Now

You can combine a for and if statement in twig, so you don't necessarily need to use search. Something like this might get you a bit closer: {% for entry in craft.entries.section('event') if entry....
9 votes

How do I set up event listing & detail pages with entry post dates that are in the future?

A better/easier route would be to just create custom Date/Time fields that store the event start and end dates. Entries’ Post and Expiry dates are meant to define when the entry should be visible on ...
9 votes
Accepted

First and Last day of Week + Month in Twig

That code in Twig would be: {% set day = now|date('w') %} {% set weekStart = now|date_modify("-#{day} days")|date('m-d-Y') %} {% set weekEnd = now|date_modify("+#{6-day} days")|date('m-d-Y') %}
8 votes
Accepted

Empty date field becomes today's date

That happens because the date filter returns the current date for null values / empty strings. If the value passed to the date filter is null, it will return the current date by default. To work ...
  • 36k
8 votes

How to order by matrix field?

My SuperSort plugin is built for use cases exactly such as this. It allows you to render each object in your array using a Twig object template and sort the array based on the rendered value.
  • 3,400
8 votes
Accepted

How to calculate the number of days left?

You want to use "a" as your DateInterval format parameter instead of "d". Also, you may need to use Twig's date function on the end date, which "converts an argument to a date to allow date ...
  • 611
8 votes
Accepted

Creating an archive page with full year and month list

{% set groupedEntries = entriesInYear | group("postDate|date('F')") %} Will give you an associative array where the keys are the months, and the values are the entries for that month. Now you need ...
6 votes
Accepted

How do I save a Plugin's date settings-field?

Yup, you need to use AttributeType::Mixed as Justin Kimbrell suggests. The date will then be escaped and stored in the database like this: * {"newregistrationdate":{"date":"2\/1\/2015"}} Now, the ...
6 votes
Accepted

How can I group entries based on a dateTime field?

You're currently grouping on the myDate field, which essentially creates a new array index for each date (e.g. 2016-01-01) which is why you're getting your current output. To group by year, this ...
6 votes
Accepted

Date/Time field outputting time with additional minutes

You are using the wrong variables. The 10 is there because it means October not 10 minutes. You have to use | date("g:ia”).
  • 3,734
6 votes
Accepted

Query entries between two values

I got the answer to this thanks to @BrandonKelly from a comment in this question: How can you return elements between two values? Turning it into a subarray does the trick: expirationDate: [ 'and', '&...
5 votes

First and Last day of Week + Month in Twig

The version below could be more understandable in some cases. First day of week (assuming Monday is the first day): {{ now | date_modify('monday this week') | date('m-d-Y') }} or alternatively: {{ ...
5 votes
Accepted

How would I create a select element with the 14 days from today as options

You're close! If there's one thing I've learned about Twig, it's easy to overthink it... Create an array with the first date, then merge the rest of dates into the same array with a for loop. Just ...
  • 6,562
5 votes
Accepted

How to compare just the date part of a dateTime field?

I'm not 100% sure I'm reading your question correctly, but here goes: I'm assuming you're getting the date as a string as a route param (maybe you shouldn't call it date though, as that is a function ...
  • 1,342
5 votes
Accepted

How do I use URL parameters to filter a channel by a date field?

You can pull query string variables using the HttpRequestService, like this: {% set month = craft.request.query('month') %} {% set year = craft.request.query('year') %} Then, you'll need to ...
5 votes

Get current date / time in specific timezone?

{{ now|date("H:i", "Europe/Paris") }} As per: http://twig.sensiolabs.org/doc/filters/date.html
  • 11.3k
5 votes
Accepted

Get current date / time in specific timezone?

First define an array of all the timezones you want to output the time with, plus what their “Standard Time”/“Daylight Savings time” abbreviations are: {% set locales = [ { timezone: 'Australia/...
5 votes
Accepted

How can I set the postDate and expiryDate from a front-end date picker via plugin?

I believe I have figured it out, please correct me so I can update the answer if I am too far off. TL;DR: I am using a date picker (no time) on a front-end form. The user chooses a date and it ...
  • 4,696
5 votes
Accepted

Escape characters in format

After trial and error I figured out you can escape characters using double backslash. {{ article.postDate.format('j\\<\\s\\u\\p\\>S\\<\\/\\s\\u\\p\\> F Y') | raw }} Outputs: 14<sup&...
5 votes

Error when submitting Date Field from the front-end: cannot be blank

The input tag presented in the comments above does not appear to use the correct input tag format for the date field. To submit a date field on the front-end, your input fields need to be setup with ...
  • 13.3k
5 votes
Accepted

date_modify relative time

You could use the diff method to get the number of days between two dates. So for example: {{ date(entry.dateCreated|date('c')).diff(now).days }} days ago In my example between today (using now) and ...
5 votes
Accepted

List Only Available Times/Dates in Solspace Calendar

Edited to provide a cleaner solution: This code will display different contents for available times and unavailable times in 30 min increments, and makes no assumption on how long your events are. {%...
  • 1,390
5 votes
Accepted

Date fields saving the wrong date from front-end form

You need to include a timezone parameter to your form in order to get the correct one. <input type="hidden" name="postDate[timezone]" value="America/Los_Angeles"> If you don't include ...
4 votes

How do I set up event listing & detail pages with entry post dates that are in the future?

As Brandon wrote, I just created a date field {{eventDate}} and then just search based on if the eventDate was later than NOW in the section named Events. {% set events = craft.entries.find({section:'...
4 votes
Accepted

How to show entries which are live between dates regardless of postdate or expiry dates?

"Live" is determined in part by postDate and expiryDate (in addition to status), so not technically independent, but this may be what you're after. You would still use the technique described in ...
4 votes

How to retrieve entries from two entry-types ordered by different date-fields?

My SuperSort Plugin is built for use cases exactly such as this. It allows you to render each object in your array using a Twig object template and sort the array based on the rendered value. To ...
  • 3,400

Only top scored, non community-wiki answers of a minimum length are eligible