phppup Posted March 1, 2012 Share Posted March 1, 2012 Does time zone change have to occur in an INI file, or can I place it as a value. I've tried using default_time_zone ('America/New York') but had no success with it, even to just ECHO a statement that says: It is now 3:50PM in New York. Quote Link to comment https://forums.phpfreaks.com/topic/258073-a-question-of-time/ Share on other sites More sharing options...
kicken Posted March 1, 2012 Share Posted March 1, 2012 The proper setting is America/New_York, with an underscore, not a space. You can change the timezone to what you want in the script whenever you want, you do not have to set it in the ini file. Quote Link to comment https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322869 Share on other sites More sharing options...
phppup Posted March 1, 2012 Author Share Posted March 1, 2012 I've tried it several times, but it never seems to work for me. How scan i simply echo: it is now xxxx in New York? Quote Link to comment https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322875 Share on other sites More sharing options...
Pikachu2000 Posted March 1, 2012 Share Posted March 1, 2012 With the date function. Quote Link to comment https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322877 Share on other sites More sharing options...
phppup Posted March 1, 2012 Author Share Posted March 1, 2012 see above... need proper syntax Quote Link to comment https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322880 Share on other sites More sharing options...
Pikachu2000 Posted March 1, 2012 Share Posted March 1, 2012 Did you read the manual entry? The syntax is well documented. Quote Link to comment https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322881 Share on other sites More sharing options...
DavidAM Posted March 1, 2012 Share Posted March 1, 2012 To set the timezone, the function name is date_default_timezone_set. // Save current timezone setting $oldTZ = date_default_timezone_get(); if (date_default_timezone_set('America/New_York')) { echo 'The current time in New York is ' . date('h:i:s a'); } else { echo ' Nobody ever knows what time it is in New York'; } // Set timezone back to what it was date_default_timezone_set($oldTZ); Quote Link to comment https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322885 Share on other sites More sharing options...
phppup Posted March 1, 2012 Author Share Posted March 1, 2012 Plenty of times, and it JUSt won't function for me. Quote Link to comment https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322887 Share on other sites More sharing options...
phppup Posted March 1, 2012 Author Share Posted March 1, 2012 Thanks DAVID. So there really is no way to just stick NY time into a database? Or does this set the timezone perminently? Quote Link to comment https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322888 Share on other sites More sharing options...
Pikachu2000 Posted March 1, 2012 Share Posted March 1, 2012 Then post the relevant code you've written, and a comprehensive explanation of the problem you're experiencing. Quote Link to comment https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322889 Share on other sites More sharing options...
phppup Posted March 1, 2012 Author Share Posted March 1, 2012 code you've written: echo "it is now $date_default_timezone_set('America/New_York')"; $NOW() comprehensive explanation of the problem: It won't provide the time. Quote Link to comment https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322894 Share on other sites More sharing options...
phppup Posted March 1, 2012 Author Share Posted March 1, 2012 Even the code from David provided me with: Fatal error: Call to undefined function: date_default_timezone_get() Quote Link to comment https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322896 Share on other sites More sharing options...
MargateSteve Posted March 1, 2012 Share Posted March 1, 2012 date_default_timezone_set('America/New_York'); $date = date('Y-m-d H:m'); echo "It is now $date"; Quote Link to comment https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322902 Share on other sites More sharing options...
DavidAM Posted March 1, 2012 Share Posted March 1, 2012 What version of PHP are you running? That function became available with PHP 5.1 I don't think you have given us a comprehensive explanation of the problem. You have suddenly injected "database" into the discussion (reply # but all of your other comments are about echoing out the time. Show us your code and tell us what it should be doing and what it is actually doing, and we can provide targeted help. echo "it is now $date_default_timezone_set('America/New_York')"; $NOW() I did not tell you to echo out the result of date_default_timezone_set() and, by the way, it is not a variable (why is there a dollar sign?) and you can not put a function call inside a quoted string (well, not if you expect it to execute); and why is $NOW() hanging out there on the end your statement all naked (without a semi-colon or anything? and what do you think it is going to do there? comprehensive explanation of the problem: It won't provide the time. Nowhere in that code did you tell it to give you the time Quote Link to comment https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322904 Share on other sites More sharing options...
phppup Posted March 1, 2012 Author Share Posted March 1, 2012 OK, so I thought it was as simple as just slapping it in there and having it echo the time on my page. So I tried: date_default_timezone_set('America/New_York'); $date = date('Y-m-d H:m'); echo "It is now $date"; and got the same error as above: Fatal error: Call to undefined function: date_default_timezone_set() but when I took out the top line: date_default_timezone_set('America/New_York'); I got a time posting, but it is Californian. I suppose there are two different things I have in mind. Fist, just to have the time display as an ECHO message. Second, if I can get it to run, I would add it to my table so I could get the NYtime that a record was added. Quote Link to comment https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322908 Share on other sites More sharing options...
MargateSteve Posted March 1, 2012 Share Posted March 1, 2012 I think David might have it spot on with the PHP version. There is a way of doing it through PHP4 (putenv I think) but before anyone puts makes any suggestions They really need to know exactly what version of PHP you are running. Quote Link to comment https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322913 Share on other sites More sharing options...
DavidAM Posted March 1, 2012 Share Posted March 1, 2012 What version of PHP are you running? That function became available with PHP 5.1 You never answered that question. and got the same error as above: Fatal error: Call to undefined function: date_default_timezone_set() It sounds like you are running an older version of PHP. If that is the case, and you cannot upgrade (which is recommended), you have to use the old timezone indicators ('EST' and 'EDT'). But that becomes tricky since you have to figure out whether the time is during Daylight Saving Time or not, first. You would have to look at using putenv and getenv to set or get the "TZ" environment variable. As to storing the date/time in the database. This can be problematic. There is no way to tell what timezone is associated with a DATE or DATETIME column in the database. The database server itself has a default timezone setting which is used as the "assumed" timezone of any DATETIME column. If this setting is changed, or you move to another server (in a different timezone), your DATETIME columns take on a different meaning. I always recommend storing dates and date-times as UTC values and applying the timezone on the front-end. If you are thinking about using different timezones for different rows of the same table, STOP NOW! That will just create a massive mess that you will never figure out later. If you just want to get past echoing out the time: echo date('Y-m-d H:m:s', strtotime(date('Y-m-d H:m:s') . ' EST')); // OR echo date('Y-m-d H:m:s', strtotime(date('Y-m-d H:m:s') . ' EDT')); Quote Link to comment https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322919 Share on other sites More sharing options...
phppup Posted March 2, 2012 Author Share Posted March 2, 2012 Well, i plugged in a TIME charachter that's NEW to 5.1 and it did NOT work. The one for 4.1 did okay though, so, being as I'm too lazy to check the server info, I'm gonna say it's 4.1 Meanwhile, I know the server and DB is on California time. Gosh, I'll have to dig up the code I once wrote that simply added 3 hours to the time (unless someone has it handy). If I remember correctly, I had to add it in a multiple of seconds or minutes (like 3 x 60x 60). Oh well, I have BIGGER issues with the damn database... LOL. Quote Link to comment https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322946 Share on other sites More sharing options...
kicken Posted March 2, 2012 Share Posted March 2, 2012 Well, i plugged in a TIME charachter that's NEW to 5.1 and it did NOT work. The one for 4.1 did okay though, so, being as I'm too lazy to check the server info, I'm gonna say it's 4.1 You shouldn't just guess. Find out for sure by calling the phpinfo() function, it will tell you all the details about your environment including version numbers. If you are on a PHP4 setup, you should seriously consider moving somewhere else or upgrading to PHP5 (5.2 minimum, 5.3 preferably) Quote Link to comment https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322949 Share on other sites More sharing options...
DavidAM Posted March 2, 2012 Share Posted March 2, 2012 or echo 'Current PHP version: ' . phpversion(); Adding 3 hours is a hack. There are two hours during the year when that will produce the wrong results. And if/when you move to a different server, if it is in a different timezone, your code will be wrong. Quote Link to comment https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322975 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.