Token module yields various php warnings if used on php 5.3.

Warnings are similar to this one:
warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting 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 'Europe/Berlin' for 'CET/1.0/no DST' instead in /home/fabio/public_html/ermanno/sites/all/modules/token/token_node.inc on line 40.

The warning is related to the use of date() in token_node.inc. Seems that with PHP 5.3 it's required to specify a default timezone before invoking date().

I found a quick and dirty fix which consists in adding date_default_timezone_set('Europe/Berlin'); (Replace Europe/Berlin with your server timezone) to token_node.inc just before this block:

if (isset($node->created)) {
        $date = (int)$node->created;
        $values['yyyy']           = date('Y', $date);
        $values['yy']             = date('y', $date);
        $values['month']          = date('F', $date);
        $values['mon']            = date('M', $date);
        $values['mm']             = date('m', $date);
        $values['m']              = date('n', $date);
        $values['ww']             = date('W', $date);
        $values['date']           = date('N', $date);
        $values['day']            = date('l', $date);
        $values['ddd']            = date('D', $date);
        $values['dd']             = date('d', $date);
        $values['d']              = date('j', $date);
      }

Hope this helps,

Fabio Varesano

Comments

fax8’s picture

I investigated some more on this issue. It's an issue Drupal is having in general regarding with PHP 5.3 support.
It's not a token only problem. It should be fixed generally in Drupal, not only under token module.

You can get more informations here http://drupal.org/node/325827

I'll keep this issue open so that other people having the same problem will find informations about that.

trevorsheridan’s picture

If anyone else is having this issue and don't like the idea of hacking into the module. The PHP documentation states (http://www.php.net/manual/en/function.date-default-timezone-set.php) that instead of using "date_default_timezone_set('UTC');", if you have access to your php.ini file you can set this value system wide. Look for the "#date.timezone" line, remove the comment, add in your timezone, and restart apache. This should suppress the errors until Drupal gets this fixed in core.

Trevor

freelylw’s picture

to fax8:

Can you be more detail for what code should be added before the 'if (isset($node->created)) {'

Sorry I don't know php much , I don't really understand what you say 'adding date_default_timezone_set('Europe/Berlin'); (Replace Europe/Berlin with your server timezone) to token_node.inc just before this block:'

Can you show the sample for what code exactly should be added into the file ? Thank you very much.

fax8’s picture

In my first comment I suggested to add before this code block:

if (isset($node->created)) {
        $date = (int)$node->created;
        $values['yyyy']           = date('Y', $date);
        $values['yy']             = date('y', $date);
        $values['month']          = date('F', $date);
        $values['mon']            = date('M', $date);
        $values['mm']             = date('m', $date);
        $values['m']              = date('n', $date);
        $values['ww']             = date('W', $date);
        $values['date']           = date('N', $date);
        $values['day']            = date('l', $date);
        $values['ddd']            = date('D', $date);
        $values['dd']             = date('d', $date);
        $values['d']              = date('j', $date);
      }

the instruction date_default_timezone_set('Europe/Berlin'); .
You have to replace Europe/Berlin with the timezone of your server. For a list see http://en.wikipedia.org/wiki/List_of_zoneinfo_time_zones

So that it would look like:

date_default_timezone_set('Europe/Berlin');
if (isset($node->created)) {
        $date = (int)$node->created;
        $values['yyyy']           = date('Y', $date);
        $values['yy']             = date('y', $date);
        $values['month']          = date('F', $date);
        $values['mon']            = date('M', $date);
        $values['mm']             = date('m', $date);
        $values['m']              = date('n', $date);
        $values['ww']             = date('W', $date);
        $values['date']           = date('N', $date);
        $values['day']            = date('l', $date);
        $values['ddd']            = date('D', $date);
        $values['dd']             = date('d', $date);
        $values['d']              = date('j', $date);
      }

Anyway I undestand that it's possible to fix this without modifying the token code.
It's possible to add the instruction date_default_timezone_set('Europe/Berlin'); in your settings.php. You can put the instruction just below the line:

<?php
// $Id: default.settings.php,v 1.8.2.4 2009/09/14 12:59:18 goba Exp $

Anyway, I hope that this issue will fixed in Drupal core soon (see linked issue in #1).

akc42’s picture

I've just hit this in the edit panel part of my web site.

Drupal does seem to have fixed this in their "format_date" function. So one possibiliy seems to me to replace each of the above date calls with something like

        $values['yyyy']           = format_date($date,'custom','Y');

I haven't looked through the token code to see why it is doing what it is doing, but a quick test with just one of the lines (the one I included above) reduces the number of error messages printed by php

dave reid’s picture

These should actually be using gmdate() or probably better yet format_date() with variable_get('date_default_timezone', 0) timezone so we don't get user account timezones used.

dave reid’s picture

Status: Active » Closed (duplicate)

This will be fixed with #307520: Date formatting function