Skip Navigation

[Resolved] Unix Timestamp Conversion for My Time Zone

This support ticket is created 3 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by Ed 3 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#1928685

Ed

Referencing this post: https://toolset.com/forums/topic/converting-a-timestamp-to-date-and-time/#post-600880

This works, but the time displayed is 6 hours later than it should be. Can I change that?

#1928739

Hello, this indicates that you are accessing the site from a timezone that is different from the site's server timezone. Normally the PHP date function will format a date based on the server's timezone settings. If you visit the site when you're in a timezone other than the server's set timezone, the date will be off by a number of hours based on your local timezone offset from the server timezone. Obviously people can access the website from any geographic location, so the solution depends on what you want to display for different Users who access the site from different timezones.

For example, if you want to display the date as in the New York USA timezone, regardless of the current User's location, you should use the PHP DateTime class to format a DateTime for that specific timezone. I found a similar example here:
https://stackoverflow.com/questions/20288789/php-date-with-timezone
The string representing the New York USA timezone is "America/New_York", instead of "Europe/London" as in the example.

Another alternative that doesn't require much modification to the existing code is to add the server's timezone abbreviation in the formatted date, so that the User can see the date is represented as for a specific timezone. Everyone who accesses the site from any location would see the same date/time information and can do their own conversion to local time.

$date = Date('F j, Y, g:i a T',$atts['timestamp']);
#1929559

Ed

Well...I'm accessing the site from Texas. The site is set to Chicago time (my time zone). I'm not sure if I understand how this is happening.

I changed the date line to the following:

$date = date('F j, Y, g:ia',$atts['timestamp'] - 5 * 60 * 60);

This is working, but I'm certain it's not the best solution.

#1929709
Screen Shot 2021-02-02 at 9.32.25 AM.png

This is working, but I'm certain it's not the best solution.
I'm not certain either based on the information I have so far. Again, it depends on what you want to display for different Users in different timezones, and the settings on the server. I'm not clear what you want to display for different Users in different timezones. The solution you have here will always show the default time offset by -5 hours, regardless of where or when the User accesses the site. So someone accessing the site from Australia will see the same date/time as you in Texas, even if it's technically "tomorrow" (as relative to "today" in Texas) when that Australian visitor visits the site. It's also a hard-coded 5 hour offset, which may or may not always be accurate because the US implements daylight savings, and the offset from UTC time varies by an hour depending on whether or not daylight savings is in effect. I think the offset is either 5 or 6 hours for central time, since it's either 4 or 5 hours for eastern time where I live. You would be able to determine the server's default timezone setting by creating a PHP Info page:
https://make.wordpress.org/support/handbook/appendix/creating-a-phpinfo-page/
Basically you create a file called phpinfo.php using a simple text editor and insert the PHP code as shown in the link above. Then use FTP to upload that phpinfo.php file to your server in the site's root directory. Once the file is uploaded, you will be able to see the PHP settings when you go to hidden link in a browser. The server's default timezone setting will be included in the settings displayed on that page - you can search for the text "timezone" to find it.

#1934371

Ed

My issue is resolved now. Thank you!