Solved

Local Date / Time

Posted on 2016-08-18
6
53 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 109

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 52

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 109

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

776 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