Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timezone Errors with post dates #2737

Closed
Tracked by #2741
asecondwill opened this issue May 16, 2023 · 9 comments
Closed
Tracked by #2741

Timezone Errors with post dates #2737

asecondwill opened this issue May 16, 2023 · 9 comments

Comments

@asecondwill
Copy link
Contributor

asecondwill commented May 16, 2023

Expected Behavior

I'm expecting the timezone setting in wp-admin to be used by the timber filters to output the time_ago

Actual behavior

My site is set to Australia/Adilade
{{ post.date('j F Y H:i') }} shows the published date in the correct timezone.

Time Ago
{{ post.post_date|time_ago }}
Time ago says "In about 7 hours" at 1230 UTC when I'm testing, again the publish date is 1930 today (16th May) - Adilaide time, which was 3 hours ago.

UPDATE: if i try
{{ post.date|time_ago }}

I get 14hours ago. which is still wrong, it's 2230 in Adilade so 3 hours ago.

Steps to reproduce behavior

Time ago
{{ post.post_date|time_ago }}

with composer twig:
"timber/timber": "2.0.0.beta-1",

Notes

I got the same behaviour in Timber 1, so upgraded thinking this had been fixed in 2 having read through the tickets here.

What version of Timber are you using?

2.0.0.beta-1

What version of WordPress are you using?

Wordpress 6.2

What version of PHP are you using?

8.0

How did you install Timber?

Installed or updated Timber through Composer

@asecondwill
Copy link
Contributor Author

Updated as I figured out the published date is

{{ post.date('j F Y H:i') }}

not
{{ post.post_date|date('j F Y')}}

@gchtr gchtr added 2.0 needs-investigation We've got all the info, now someone needs to look into this to figure out what's going on labels May 16, 2023
@gchtr
Copy link
Member

gchtr commented May 16, 2023

Most of the date functionality was fixed in 2.x, but it could very well be that I missed the |time_ago() filter. We will first have to add a test to confirm this is still an issue.

@gchtr gchtr added the help wanted Do you know computer? Then lend a hand and have your code live in infamy! label May 17, 2023
@gchtr gchtr mentioned this issue May 17, 2023
30 tasks
@Levdbas
Copy link
Member

Levdbas commented May 18, 2023

Did some local tests @gchtr and I recreated the same two outcomes that @asecondwill describes be it with different dates and time zones.

{{ post.post_date|time_ago }} does not seem to take the timezone into account because it relies on time() instead of using current_time() see WP docs

{{ post.date }} as stated in the Timber docs only returns the publication date without the time. So when using |time_ago on that it will always count use that day at midnight. That in combination with our first issue gives leads to inexplicable behavior. Using {{post.date('j F Y h:i')|time_ago}}, so explicitly force hours and minutes in, fixes this issue as well. So when you want to use |time_ago, it's best to use post_date.

@gchtr gchtr self-assigned this May 30, 2023
@gchtr gchtr removed help wanted Do you know computer? Then lend a hand and have your code live in infamy! needs-investigation We've got all the info, now someone needs to look into this to figure out what's going on labels May 30, 2023
@gchtr
Copy link
Member

gchtr commented May 31, 2023

I found out what the issue was and added a fix in #2758.

{{ post.date }} as stated in the Timber docs only returns the publication date without the time. So when using |time_ago on that it will always count use that day at midnight. That in combination with our first issue gives leads to inexplicable behavior. Using {{post.date('j F Y h:i')|time_ago}}, so explicitly force hours and minutes in, fixes this issue as well.

Correct. You should use a full date and time string when using {{ post.date }}. But you don’t necessarily have to use j F Y H:i:s. You can also use one of the following:

{{ post.date('U')|time_ago }}
{{ post.date(constant('DATE_ATOM'))|time_ago }}
  • U is a timestamp
  • DATE_ATOM is an example for using a predefined timezone constant. See The DateTimeInterface interface. You could also use DATE_W3C or DATE_RFC3339. I just feel like DATE_ATOM is the easiest to remember.

So when you want to use |time_ago, it's best to use post_date.

You can use {{ post.post_date|time_ago }}. That should work just as fine for the |time_ago filter. The only difference to using {{ post.date('U')|time_ago }} is that no filtering will be applied.

{{ post.post_date|time_ago }} does not seem to take the timezone into account because it relies on time() instead of using current_time().

Just a note here. As of WordPress 5.3, it’s not recommended to use current_time() anymore when you want the current time. See Date/Time component improvements in WordPress 5.3. The correct usage to work with time in Timber is document in the Date/Time Guide.

@alessandro-newzoo
Copy link

@gchtr just wanted to let you know the results of my tests too, as they're all pretty inconsistent.

  • Timber version: 2.0.0-beta.2
  • WP version: 6.2.2
  • WP settings timezone: Europe/Amsterdam

I have an article I published 15 minutes ago.

Let's start from your suggestions:

{{ post.date(constant('DATE_ATOM'))|time_ago }} : 15 minutes ago

{{ post.date('U')|time_ago }} : 53 years ago

{{ post.post_date|time_ago }} : 2 hours from now

Other tests:

{{ post.date|time_ago }} : 12 hours ago

{{post.date('j F Y h:i')|time_ago}} : 10 hours ago

@gchtr
Copy link
Member

gchtr commented Jun 6, 2023

@alessandro-newzoo My suggestions don’t work yet. But they should as soon as the pull request #2758 is merged.

Maybe you can check it out when you test the fix by installing from the pull request’s branch?

composer require timber/timber:dev-2.x-time-ago

@alessandro-newzoo
Copy link

@gchtr ohhh sorry about that, I misinterpreted your previous comment!
I don't have a lot of time this week to test another version, but I might give it a shot next week.

For now, for some reason, {{ post.date(constant('DATE_ATOM'))|time_ago }} seems to work perfectly, so I'll use that for the time being.

Thank you SO much both for the PR and for providing a snippet that finally made me stop scratching my head after 2 days haha!

@gchtr
Copy link
Member

gchtr commented Jun 6, 2023

@alessandro-newzoo That’s alright. I figured the first line in my comment can easily missed :).

For now, for some reason, {{ post.date(constant('DATE_ATOM'))|time_ago }} seems to work perfectly, so I'll use that for the time being.

I think that works because the DATE_ATOM format includes the timezone in the returned date, so the underlying strtotime() can interpret it correctly when you use |time_ago.

Thank you SO much both for the PR and for providing a snippet that finally made me stop scratching my head after 2 days haha!

You’re welcome! Dates and time made me scratch my head a lot in the past. I took quite some time to eventually "get it".

@gchtr
Copy link
Member

gchtr commented Jun 9, 2023

Fixed in #2758.

@gchtr gchtr closed this as completed Jun 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants