Link to home
Start Free TrialLog in
Avatar of breeze351
breeze351

asked on

php time

This is a stupid one.
It is now May 21st 2017 at 5:39:pm
The following code:
[code]
$today = date("m/d/Y")." ".date("h:i:sa");
[code]

Returns:
05/21/2017 03:54:34pm

I'm not worried about the minutes, but why is it giving me "Mountain" time?  I'm on the East coast.
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Check your default timezone:
date_default_timezone_get()

If you want east coast time, do:
date_default_timezone_set("America/New_York");
You can also change the default timezone in your PHP.ini file.
Avatar of breeze351
breeze351

ASKER

I don't want to check time zones.  It's a web site.  I would have no idea where you are.
Bookmark these two articles and refer to them when you have questions about how to handle date/time values in PHP.

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

Object-oriented (you should be using OO now)
https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html

If you create a new DateTime object and do not tell it the timezone, it will assume UTC (same as Greenwich Mean Time).  Then you can inject your preferred timezone into the object.  Here's an example.
https://iconoun.com/demo/temp_breeze351.php
<?php // demo/temp_breeze351.php
/**
 * https://www.experts-exchange.com/questions/29024216/php-time.html
 *
 * https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
 * http://php.net/manual/en/datetime.settimezone.php
 */
error_reporting(E_ALL);
echo '<pre>';

// START WITH A PRISTINE OBJECT
$dto = new DateTime;
var_dump($dto);

// SET A TIMEZONE
$zone = new DateTimeZone('America/Chicago');
$dto->setTimeZone($zone);
var_dump($dto);

Open in new window

Outputs
object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2017-05-21 22:17:53.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(3) "UTC"
}
object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2017-05-21 17:17:53.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(15) "America/Chicago"
}

Open in new window

Sidebar note... For the east coast of USA the correct timezone is "America/New_York" as shown here:
http://php.net/manual/en/timezones.php
http://php.net/manual/en/timezones.america.php

Timezone handling is really improved in PHP's object-oriented Date/Time implementation.  Much easier than the old procedural ways!
I've looked at all the replies and Ray's links.  The only thing that I can see is if you have a web site running php, there is no way to determine the correct time and display it.  So that "Mountain Time" that I referred to above is coming from the server.
Are my assumptions correct?
The server can't automatically tell you the correct time for the visitor because that information isn't sent to the server by default. It's like calling up a random person and asking them to tell you what time it is. The person knows what the time is in THEIR timezone but they won't know your timezone unless you tell them.

If you want a web page to be able to display the visitor's local time, you can use JavaScript but that's not a reliable piece of information since anyone can screw with their own system's clock.
SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And yes, Mountain Time is probably coming from the server.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, I'll refer to this again later.  Right now I have so much shit on my list the easiest way to solve this is to delete the time stamp from the print header.
Thanks again