Avatar of Richard Korts
Richard KortsFlag for United States of America

asked on 

Local Date / Time

In php programs, I have for a long time used this:

date_default_timezone_set("America/Los_Angeles");

To get the date / time in Pacific Time, USA. Other parameters for US Central, etc.

I am doing some custom pages on a word press site; it does not seem to work.

I seem to get GMT.

Does that not work in WordPress?

The site is hosted at WPEngine. Could that be the problem? I think they operate their servers on GMT

Of course I can "manually" convert to Pacific Time by adding 8 hours.
WordPressPHP

Avatar of undefined
Last Comment
Ray Paseur
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Where in your code is the date_default_timezone_set() function called?  If it is called before using the date/time functions, it will have the appropriate effect.  If the values from the date/time functions are set and stored before the function call, you will get the default values.  You can use phpinfo() to determine what timezone settings are in effect at the time your date/time functions are called.
Avatar of Kyaw Wanna
Kyaw Wanna
Flag of Thailand image

Please try the code below :
$date_format = get_option( 'date_format' );
$time_format = get_option( 'time_format' );
echo date( "{$date_format} {$time_format}", current_time( 'timestamp' ) );

Open in new window

Avatar of Richard Korts
Richard Korts
Flag of United States of America image

ASKER

Ray:

The date_default_timezone_set() function is the first line of code.

Richard
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Are you trying to use time zones when somebody has logged in and they indicated in a form tha was submitted they are in NY, Chicago or LA?   OR do you want to be able to show content based on a non logged in users location?

If the latter, then you can use javascript and keep in mind that your php code will run on the server, then javascript will run after the html is generated. http://www.w3schools.com/jsref/jsref_gettimezoneoffset.asp and I have created a slightly different function that will convert to hours and remember our friends that are offset in fractions of an hour
http://jsbin.com/xuziwojaze/edit?html,output
<html>
<head>
  <script src="https://code.jquery.com/jquery-2.2.4.js"></script>
   <script>
     $(function(){
  $('.result').text(getUTCDifference() );
});

function getUTCDifference() {
    var d = new Date();
    var n = d.getTimezoneOffset();
    var h = n / 60; // hours
    var f = h.toFixed(2);  // http://www.timeanddate.com/time/time-zones-interesting.html
    
    return f; // in hours, 2 decimal places
}
  </script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <div class="result"></div>
  <div>
    <p>Your time zone is offset by <span class="result"></span> hours</p>
  </div>
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo