Solved

Local Date / Time

Posted on 2016-08-18
6
38 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
6 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Comment Utility
Ray:

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

Richard
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
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 82

Accepted Solution

by:
Dave Baldwin earned 300 total points
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now