Solved

Local Date / Time

Posted on 2016-08-18
6
68 Views
Last Modified: 2016-08-22
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.
0
Comment
Question by:Richard Korts
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41761786
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.
0
 
LVL 2

Expert Comment

by:Kyaw Wanna
ID: 41761852
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

0
 

Author Comment

by:Richard Korts
ID: 41761894
Ray:

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

Richard
0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 41761939
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

0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 300 total points
ID: 41762079
Most Linux / Unix servers use GMT.  You have to specify a local timezone if you want one.  You might want to see if there is another date_default_timezone_set() somewhere else in the code.
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 41762504
I think Dave's instincts are good about looking for other code that changes the local timezone settings.  In WordPress, there is a thing called "the loop."  It controls logic flow in ways that are not always clear to those of us who come from a background of building applications where we  control the logic flow.  So you might be putting your own code into the mix, but because of the loop  your code is getting executed before or after you expect it to be run.

Information on how to handle date/time values in PHP and MySQL is collected in these articles.

Procedural:
https://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL-Procedural-Version.html

Object-oriented:
https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question