Jump to content

Timestamp value of date field always -1 day in the past


Recommended Posts

I was upgrading some date formatting code lines in my projects and noticed a strange behaviour.

I have a date field in the backend which is a classic processwire datetime field. In this field I can set - for example - an event date. For date selecting I am using the HTML5 Datepicker option in the field settings. Besides that I changed none of the fields settings at all.

To display this date in the frontend all i do is:

echo §page->event_date;

However - I need to format this date in multiple ways throughout the whole site. So I need a unix timestamp (for localization of the date).

strtotime($page->event_date);

When I check the timestamp that comes out - it is always exactly 1 day before the date that was set in the date field.

So when selecting "08.06.22" in the date field, the timestamp says "07.06.22"

When I format the date -based on the timestamp- to make it human-readable again I can confirm that the new date is one day in the past...

How is this possible?

Link to comment
Share on other sites

I think this has to do with your time zone 😉

Do you have the Language Support module installed?
If yes, go to Administration -> Languages and select the appropriate language

Then edit the "wire--modules--languagesupport--languagesupport-module.json" file and set your locale in "C" (example: de_DE.UTF-8)

 

Can you post a timestamp as an example?

  • Like 1
Link to comment
Share on other sites

32 minutes ago, zoeck said:

I think this has to do with your time zone 😉

Do you have the Language Support module installed?
If yes, go to Administration -> Languages and select the appropriate language

Then edit the "wire--modules--languagesupport--languagesupport-module.json" file and set your locale in "C" (example: de_DE.UTF-8)

 

Can you post a timestamp as an example?

My config.php looks like this:

$config->timezone = 'Europe/Berlin';
setlocale(LC_ALL,  'de_DE.utf8', 'de_DE');

I edited the json file in the Backend (with: de_DE.UTF-8).

The timestamp looks like this: 1654898400

When examining this timestamp I get both dates:

GMT: Friday, 10. June 2022 22:00:00
Your time zone: Samstag, 11. Juni 2022 00:00:00 GMT+02:00 DST

It seems that I just ignored that fact that there are two dates shown (always look at the first one).

My $page->testdate field outputs the date like this: 11.06.2022

So this outputs the right date (11. Juni):

<?= strftime("%e. %B", strtotime($page->testdate)); ?>

My problem is that this is deprecated in the future and the new way to do this looks like this:

 <? echo IntlDateFormatter::formatObject( new DateTime($page->testdate), "dd. MMM", 'de_DE'); ?>

This also outputs 11. Juni (correct)

But when using the timestamp I get the wrong date:

<? echo IntlDateFormatter::formatObject( new DateTime('@' . strtotime($page->testdate)), "dd. MMM", 'de_DE' ); ?>

This outputs 10. Juni

Link to comment
Share on other sites

I found the reason: https://www.php.net/manual/en/datetime.construct.php

The $timezone parameter and the current timezone are ignored when the $datetime parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).

So thats why i get the wrong date when using this code here (my timezone gets ignored)

<? echo IntlDateFormatter::formatObject( new DateTime('@' . strtotime($page->testdate)), "dd. MMM", 'de_DE' ); ?>

Even when explicitly setting the timezone inside the DateTime object constructor it gets ignored.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...