?
Solved

Local Date / Time

Posted on 2016-08-18
6
Medium Priority
?
77 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 111

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
Understanding Web Applications

Without even knowing it, most of us are using web applications on a daily basis. Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We often confuse these web applications tools for websites.  So, what is the difference?

 
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 84

Accepted Solution

by:
Dave Baldwin earned 1200 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 800 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

Simplify Your Workload with One Tool

How do you combat today’s intelligent hacker while managing multiple domains and platforms? By simplifying your workload with one tool. With Lunarpages hosting through Plesk Onyx, you can:

Automate SSL generation and installation with two clicks
Experience total server control

Question has a verified solution.

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

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

719 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