Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322885
Share on other sites

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 #8) 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

 

Link to comment
https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322904
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322908
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322919
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322946
Share on other sites

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)

Link to comment
https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322949
Share on other sites

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/258073-a-question-of-time/#findComment-1322975
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.