Full support for Drupal time and date
meatbites - December 6, 2007 - 09:58
| Project: | Token |
| Version: | 5.x-1.10 |
| Component: | Code |
| Category: | bug report |
| Priority: | normal |
| Assigned: | meatbites |
| Status: | patch (code needs review) |
Description
I've noticed Pathauto annoyingly uses the server time to generate a creation date/time -- as opposed to going through Drupal's own time zone filter which uses the /admin/settings/date-time setting.
If I get the time to look at it, I will, but unfortunately that won't be for a while, so I thought I'd file a bug report to at least make you aware of the issue, as it sure would be a helpful fix.
Apologies if this is a duplicate.

#1
This is a bug against token module probable. token_node.inc would be the file to look at.
Questions: should the token use the site time zone? The time zone of the user creating the node? The time zone of the user editing the node?
I agree that "server time" is wrong, but I'm not sure there is an easy answer on which is right. Perhaps the api for making timezone adjusted dates takes a lot of this into account already. I haven't looked.
#2
With this patch, Token now takes advantage of Drupal's format_date. This addresses both the lack of timezone support and any other time and date settings Drupal may have both now and in future.
Toggling user-configurable timezones is indeed an option already built into Drupal. A further option specific to Token could probably be added later, but I think that's a feature outside of this issue as the functionality is already there.
#3
subscribing
#4
I just had a quick play. The following might pave way to a better implementation, as I'm not sure the multiple calls to format_date is entirely efficient. I've not yet tested this under token.
<?php
$date_types_index = array(
'yyyy' => 'Y',
'yy' => 'y',
'month' => 'F',
'mon' => 'M',
'mm' => 'm',
'm' => 'n',
'ww' => 'W',
'date' => 'N',
'day' => 'l',
'ddd' => 'D',
'dd' => 'd',
'd' => 'j'
);
// Token date formats mapped to their respective formatted date values (i.e.: 'yyyy' => '2008').
$date_types = array_combine(
array_keys($date_types_index),
explode(',', format_date($node_created, 'custom', implode(',', $date_types_index)))
);
foreach ($date_types as $key => $value) {
$values[$key] = $value;
}
?>
Thoughts?
#5
Another quick fiddle -- this would be close to the final version, I'd think. I'll eventually get to properly testing this and putting it into a patch, unless someone beats me to it.
<?php
// Returns an array of date format keys mapped to their respective drupal-formatted date values.
function _token_get_date_values($date, $date_type = 'created') {
// Node changed date.
if ($date_type == 'changed') {
$date_types_index = array(
'mod-yyyy' => 'Y',
'mod-yy' => 'y',
'mod-month' => 'F',
'mod-mon' => 'M',
'mod-mm' => 'm',
'mod-m' => 'n',
'mod-ww' => 'W',
'mod-date' => 'N',
'mod-day' => 'l',
'mod-ddd' => 'D',
'mod-dd' => 'd',
'mod-d' => 'j'
);
}
// Node created date (default).
else {
$date_types_index = array(
'yyyy' => 'Y',
'yy' => 'y',
'month' => 'F',
'mon' => 'M',
'mm' => 'm',
'm' => 'n',
'ww' => 'W',
'date' => 'N',
'day' => 'l',
'ddd' => 'D',
'dd' => 'd',
'd' => 'j'
);
}
return array_combine(
array_keys($date_types_index),
explode(',', format_date($date, 'custom', implode(',', $date_types_index)))
);
}
if (isset($node->created)) {
$values = array_merge($values, _token_get_date_values($node->created, 'created'));
}
if (isset($node->changed)) {
$values = array_merge($values, _token_get_date_values($node->changed, 'changed'));
}
?>
#6
See attached patch for the final working version of my above code. It's slightly faster than calling drupal_format multiple times.
Note: I added back the conditionals originally inserted here that leave the dates empty if they're not needed. These seemed to mysteriously disappear from HEAD -- was this intentional? If so, I'll rework a patch without the function.
#7
Re-rolled the patch to suit Token 5.x-1.10.
Additional:
- Added (int) cast to incoming date (as per this version of Token).
- Minor whitespace fix.