PHP Date()
Moderator: General Moderators
PHP Date()
I'm using the date() function. My clock on my local machine is set to New York time, and when i run date("H:i:s"); it's 4 hours ahead of what it's suppose to be. So i use date_default_timezone_set("America/New-York"); to make sure I'm not doing anything incorrectly, and still it's 4 hours ahead. What is going on?
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
Re: PHP Date()
I've had better luck with creating a new datetime object
http://php.net/manual/en/timezones.america.php
You also might want to just do a date_default_timezone_get before and after your call to see if it is changing. Some versions of PHP have bugs related to timezones and date(). Sometimes the timezone database isn't installed -- look with Sometimes if the timezone isn't properly configured to start with then changing the timezone fails.
Code: Select all
$timezone = new DateTimeZone( "America/New_York" );
$date = new DateTime();
$date->setTimezone( $timezone );
echo $date->format( 'H:i:s A / D, M jS, Y' );You also might want to just do a date_default_timezone_get before and after your call to see if it is changing. Some versions of PHP have bugs related to timezones and date(). Sometimes the timezone database isn't installed -- look with
Code: Select all
phpinfo(INFO_MODULES); Re: PHP Date()
Directive Local Value Master Value
date.default_latitude 31.7667 31.7667
date.default_longitude 35.2333 35.2333
date.sunrise_zenith 90.583333 90.583333
date.sunset_zenith 90.583333 90.583333
date.timezone UTC UTC
It seems to be configured correctly
Your example works correctly displays my time the right way, though i don't know why date() won't just do what it should. Anyhow, thanks for the example!
date.default_latitude 31.7667 31.7667
date.default_longitude 35.2333 35.2333
date.sunrise_zenith 90.583333 90.583333
date.sunset_zenith 90.583333 90.583333
date.timezone UTC UTC
It seems to be configured correctly
Your example works correctly displays my time the right way, though i don't know why date() won't just do what it should. Anyhow, thanks for the example!
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
Re: PHP Date()
"America/New-York" is not a valid timezone identifier (but "America/New_York" is). If given an invalid timezone, date_default_timezone_set() will trigger an error. If you don't see the error, it is because display_errors is disabled or the error_reporting level is too restrictive.
Re: PHP Date()
Also, just to verify, you mention "local machine", which to me says you are also working with a remote machine, if this is the case, is the time set correctly on it (just in case maybe it is one you configured as a test environment, most hosting places (one would hope) would be correct 
Re: PHP Date()
Still 4 hours ahead. I figured out my mistake in New-York so now it works when i define the timezone. If I don't, well it's 4 hours ahead, even on my local machine while my time is correct.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
Re: PHP Date()
If you want the timezone setting to be permanent, you need to change date.timezone in php.ini.
Re: PHP Date()
Done that. Honestly idk. I give up, I'm just going to declare my timezone once on the applications I make, make it easier on me.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
- flying_circus
- Forum Regular
- Posts: 732
- Joined: Wed Mar 05, 2008 10:23 pm
- Location: Sunriver, OR
Re: PHP Date()
I set it manually on applications I develop, it's typically the first line in my config file.Pazuzu156 wrote:Done that. Honestly idk. I give up, I'm just going to declare my timezone once on the applications I make, make it easier on me.
Code: Select all
<?php
# Timezone
date_default_timezone_set('America/Los_Angeles');
?>I was just pondering this the other day, I was tempted to normalize all of my php projects (regardless of where the server is located) to zulu time. Atleast then you'd have consistency between your apps.
Re: PHP Date()
[quote=flying_circus]I set it manually on applications I develop, it's typically the first line in my config file.[/quote]
I'm ending up doing the same thing, it will wind up being second nature to me like session_start() etc. :p
I'm ending up doing the same thing, it will wind up being second nature to me like session_start() etc. :p
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156