Converting datetimeformat for StringToTime

SOLVE
Highlighted
Occasional Contributor

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)

Reply
0 Upvotes
1 Accepted solution

Accepted Solutions
Highlighted
Advisor | Partner

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!

3 Replies 3
Highlighted
Advisor | Partner

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!

Highlighted
Occasional Contributor

I see what you did. It works!

 

I thought you have to use the datetimeformat filter to actually ge the time. Here's my code with examples for today, yesterday, 3-5 days ago, last week, and then published date. I probably coudl've simplified by getting the between_times filter to give out days instead of hours.

 

{% set posts = blog_recent_posts('default', 200) %}
{% for post in posts %}

Published {% set publishDate = post.publish_date %} {% set currentDate = local_dt %} {% set begin = publishDate %} {% set hours_since_publish_date = begin|between_times(currentDate, 'hours') %} {% set days_since_publish_date = (hours_since_publish_date / 24)|round %} {% 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 and hours_since_publish_date < 120 %} <time class="timeStamp" datetime="{{ post.publish_date|datetimeformat('%B %e, %Y') }}">{{ days_since_publish_date }} days ago</time> {% elif hours_since_publish_date >= 120 and hours_since_publish_date < 240 %} <time class="timeStamp" datetime="{{ post.publish_date|datetimeformat('%B %e, %Y') }}"> last week</time> {% elif hours_since_publish_date >= 240 %} <time class="timeStamp" datetime="{{ post.publish_date|datetimeformat('%B %e, %Y') }}">{{ post.publish_date|datetimeformat('%B %e, %Y') }}</time> {% endif %} by <a href="#" class="authorText">{{ post.blog_author.display_name }}</a>
{% endfor %}
Highlighted
Advisor | Partner

Awesome!

 

Yeah filters can be very confusing. Not too long ago I made the exact same mistake you did haha so I'm very happy to help out.

 

The retrospective is my favorite part of development. You will almost always find a better more efficient wat to handle your logic!