PHP Date()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

PHP Date()

Post by Pazuzu156 »

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
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: PHP Date()

Post by Eric! »

I've had better luck with creating a new datetime object

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' );
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

Code: Select all

phpinfo(INFO_MODULES); 
Sometimes if the timezone isn't properly configured to start with then changing the timezone fails.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: PHP Date()

Post by Pazuzu156 »

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!
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: PHP Date()

Post by McInfo »

"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.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: PHP Date()

Post by twinedev »

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 ;-)
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: PHP Date()

Post by Pazuzu156 »

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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: PHP Date()

Post by McInfo »

If you want the timezone setting to be permanent, you need to change date.timezone in php.ini.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: PHP Date()

Post by Pazuzu156 »

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
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: PHP Date()

Post by flying_circus »

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.
I set it manually on applications I develop, it's typically the first line in my config file.

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.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: PHP Date()

Post by Pazuzu156 »

[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
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
Post Reply