CMS Development

david-at-bold
Membro

Converting datetimeformat for StringToTime

resolver

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)

0 Avaliação positiva
1 Solução aceita
Kevin-C
Solução
Especialista reconhecido(a) | Parceiro
Especialista reconhecido(a) | Parceiro

Converting datetimeformat for StringToTime

resolver

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!

Kevin Cornett - Sr. Solutions Architect @ BridgeRev

Exibir solução no post original

4 Respostas 4
Dhemes
Membro

Converting datetimeformat for StringToTime

resolver

Using DateFormat. format() method
Get the date to be converted.
Create an instance of SimpleDateFormat class to format the string representation of the date object.
Get the date using the Calendar object.
Convert the given date into a string using format() method.
Print the result.

 

Regards,

Will

0 Avaliação positiva
Kevin-C
Solução
Especialista reconhecido(a) | Parceiro
Especialista reconhecido(a) | Parceiro

Converting datetimeformat for StringToTime

resolver

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!

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
david-at-bold
Membro

Converting datetimeformat for StringToTime

resolver

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 %}
Kevin-C
Especialista reconhecido(a) | Parceiro
Especialista reconhecido(a) | Parceiro

Converting datetimeformat for StringToTime

resolver

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!

Kevin Cornett - Sr. Solutions Architect @ BridgeRev