简体   繁体   中英

php date() function displaying wrong time

I'm building a site with wordpress, and when I use the date() function on an otherwise empty file, it shows the right date. However, if I use it in a file that includes other wordpress files, the time is displayed as GMT time, so the date is off.

If anyone can figure out how I can use date function without interruption from the wordpress methods, I'd appreciated it.

I Dont use word press so im not sure but it sounds like there is a setting in wp that is applied using date_default_timezone_set() . Its probably configured to GMT to allow for easy calculation of user preferences from within WP. I would look to see if this is configurable in WP, or do the math youself using the necessary functions and/or objects.

The Wordpress template tag the_time displays according to your Wordpress defaults: Template Tags/the time « WordPress Codex . date() may get over written by Wordpress. But you can look at the_time usage and code samples to change the output more to what you need: Formatting Date and Time « WordPress Codex

Try using the date_i18n() function available in Wordpress, I had a similar problem and used that function rather than date(). Check out the WP docs - http://codex.wordpress.org/Function_Reference/date_i18n

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM