- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
Converting datetimeformat for StringToTime
SOLVEJan 3, 2020 12:34 PM
Hey, I'm trying to print out the time of a published post depending on how many days it has been since it was posted. After researching quite a bit, I'm stuck at trying to pass on the variable publishDate so I can use the between_times filter to figure out the time between 2 dates.
Here is my code:
Published</br> {% set publishDate = post.publish_date|datetimeformat('%Y-%m-%d %H:%M:%S') %} {% set currentDate = local_dt|datetimeformat('%Y-%m-%d %H:%M:%S') %} {% set begin = publishDate|strtotime("yyyy-MM-dd'T'HH:mm:ssZ") %} {% set end =currentDate|strtotime("yyyy-MM-dd'T'HH:mm:ssZ") %} {% set hours_since_published_date = begin|between_times(end, 'hours') %} {% if hours_since_publish_date < 24 %} <time class="timeStamp" datetime="{{ post.publish_date|datetimeformat('%B %e, %Y') }}">today</time> {% elif hours_since_publish_date >= 24 and hours_since_publish_date < 48 %} <time class="timeStamp" datetime="{{ post.publish_date|datetimeformat('%B %e, %Y') }}">yesterday</time> {% elif hours_since_publish_date >= 48 %} <time class="timeStamp" datetime="{{ post.publish_date|datetimeformat('%B %e, %Y') }}">{{ hours_since_publish_date}} days ago</time> {% endif %} by <a href="#" class="authorText">{{ post.blog_author.display_name }}</a>
I end up getting errors with the following two lines: strongToTime could not match dateTime input with dateTime format.
{% set begin = publishDate|strtotime("yyyy-MM-dd'T'HH:mm:ssZ") %} {% set end = currentDate|strtotime("yyyy-MM-dd'T'HH:mm:ssZ") %}
I've been trying to figure out how I can convert a dateTIme into a dateTIme object (or if that's even possible)
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Accepted Solutions
Jan 5, 2020 7:29 PM - edited Jan 6, 2020 12:41 PM
Hey @david-at-bold
I'd recommend not transforming any data until it's time to print it or assigning unneeded variables. Transforming before a comparrsion will make it a lot harder to debug than it has to be.
Your code also has 2 major issues:
1) You've missepelled your hours varieble you set it as "hours_since_published_date" then later try to reference/use it as "hours_since_publish_date".
2) Also on line 5 of the code block you posted you're missing a space when declaring the "end" variable.
I used this code to test, and it seems to work as expected:
{% set pop_posts = blog_recent_posts('default', 100) %} {% for post in pop_posts %} {% set publishDate = post.publish_date %} {% set currentDate = local_dt %} {% set begin = publishDate %}
{% set hours_since_publish_date = begin|between_times(currentDate, 'hours') %} {% if hours_since_publish_date < 10000 %} <time class="timeStamp" datetime="{{ post.publish_date|datetimeformat('%B %e, %Y') }}">today</time> {% elif hours_since_publish_date >= 24 and hours_since_publish_date < 48 %} <time class="timeStamp" datetime="{{ post.publish_date|datetimeformat('%B %e, %Y') }}">yesterday</time> {% elif hours_since_publish_date >= 48 %} <time class="timeStamp" datetime="{{ post.publish_date|datetimeformat('%B %e, %Y') }}">{{ hours_since_publish_date}} days ago</time> {% endif %} {% endfor %}
You'll see the for loop I added for my testing purposes
Hope this helps get you moving!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content