using mktime correctly

Events happening in the community are now at Drupal community events on www.drupal.org.
jredding's picture

In userpoints.module at line 1537 we have the following function

function userpoints_date_to_timestamp($date) {
  //This takes the FAPI date form array and returns a timestamp
  if ($date) {
    return mktime(0, 0, 0, $date['month'], $date['day'], $date['year']);
  }
}

The function works just fine and does exactly what its supposed to do BUT it throws an exception when run through simpletest. The following error is reported

mktime() [function.mktime]: It is not safe to rely on the system's timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EST/-5.0/no DST' instead

I understand we can safely ignore this but I would rather not. We do have

variable_get('date_default_timezone', 0);

so we can get the site's default timezone and use this.

anyhow what's the proper way of getting rid of this error?

Comments

When I encountered this for

kbahey's picture

When I encountered this for a site that had extremely old code base and wanted to run on PHP5, I used the following:

<?php

ini_set
('date.timezone', 'America/New_York');
?>

Or better yet:

<?php
if (!ini_get('date.timezone')) {
 
ini_set('date.timezone', 'America/New_York');
}
?>

However, this imposes the above timezone on all site users, which may not work for a site that allows users to have different time zones. Since we are doing "days" and not hours, this may be OK.

If you want to take it further.

In Drupal itself, we have the following variables: configurable_timezones (true/false), sitewide_timezone (?). and date_default_timezone, which contains something like s:6:"-14400"; (number of seconds offset from UTC). In addition to that the users table has a timezone column like so -18000 (also UTC offset).

So, ideally, it would be something that checks the timezones, you can check if $user->timezone has a value, and if not, use variable_get('date_default_timezone', 0). But we need to convert that, via a table, to a timezone string. See comments here http://www.php.net/manual/en/function.date-default-timezone-set.php

Drupal performance tuning, development, customization and consulting: 2bits.com, Inc..
Personal blog: Baheyeldin.com.

Drupal performance tuning, development, customization and consulting: 2bits.com, Inc..
Personal blog: Baheyeldin.com.

This worked for me

shunting's picture

When I put the snippet:

if (!ini_get('date.timezone')) {
  ini_set('date.timezone', 'America/New_York');
}

At the very beginning of index.php (I know, hackish no-no!).

But I'm running D6.13, and the error's thrown at line 848 of node module, so it's not all down to an old code base.

Same problem with

summit's picture

Same problem with token_node.inc. Line 48 problem with timezone. Filed a reply on: http://drupal.org/node/307520#comment-2448248. Greetings, Martijn

Another solution

uberhacker's picture

Put the following in settings.php:

ini_set('date.timezone', date_default_timezone_get());

"God gave us two ears and one mouth to remind us we should listen twice as much as we talk."

Great solution. Thanks for

gawesha's picture

Great solution. Thanks for sharing. It's solved my timezone issue in token module. Think this is better than hacking modules.

A million Thanks --- fixed

wekem's picture

A million Thanks --- fixed the terrible error!

Best solution

julioensivar's picture

thanks for this! we really appreciate it!

Best Solution!

alextitoff's picture

It works!
Many thanks.

Thank you very much

hoangbien's picture

it work fine. Thank you very much,

HOANG BIEN

Didn't work for me

pmbd's picture

Had this problem on several sites, and the above fix just made things worse (additional errors.) Adding date.timezone = "America/Los_Angeles" to php.ini did the trick though.

Thank You !!!!

dmon000's picture

Worked Perfectly!!

User Points

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week