Convert basic date string to dateobject (no time in string)

Regular Contributor



I'm have an issue. We have event posts that have basic single line text field. In that the content publisher is putting in the date like so (d-m-Y e.g. 03-09-2019).


I've tried to use strtotime to convert this string into a dateobject which I can then use to convert into reading somethign like 3 Sept 19.


I think the issue is strtotime doesn't like that there's not time e.g.


{% set edate = post.widgets.event_date.body.value|strtotime('d/m/Y') %}

<p>{{ edate|datetimeformat('%e') }}</p>
1 Reply 1

Hey @benji_thecroc 


As with most of my post(lol)…I'm no expert, but I gave it a shot!


It looks like your "strtotime" arguments are formated incorrectly.


As per the docs your year should be labeled as "yyyy", month as "MM", and day as "dd". All separated by "-". The user's input should be formated in this way as well.


It is also missing the time stamp which unfortunately is require. You could append a generic timestamp dynamically if it is not needed on your end.


Maybe something like this?


{# adding a generic timestamp #}
{% set inputDate = post.widgets.event_date.body.value~"T00:00:00+0000" %} 

{# declare edate #}
{% set edate = inputDate|strtotime("yyyy-MM-dd'T'HH:mm:ssZ")|unixtimestamp %} 

{# print value #}
<p>{{ edate|datetimeformat('%e') }}</p>



I hope this helps!