Use different date format
jramirez - May 3, 2008 - 08:08
| Project: | Scheduler |
| Version: | 5.x-1.13 |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
Description
I'd like to use scheduler but in stead of using:
'Y-m-d H:i:s' date format, I'd like to use 'd-m-Y H:i:s' or 'd-m-Y'
It is that possible?
Thanks.

#1
I am sorry, but currently you have to use 'Y-m-d H:i:s'.
But scheduler really should support different date formats.
#2
To support different date formats we need a better strtotime().
I don't like any means of input which must follow a certain format. Maybe a better way to input dates and times would be a separate input control for each format entity (e.g. a seperate input control for day, month, year, hour, minute and second). This prevents the user from inputting a wrong format instead of whining afterwards. The input controls can even be dropdowns or lists with all availlable numbers, like 1-12 for the month. The downside of this approach is the incompatibility with ajax calendar controls.
Opinions?
#3
I agree that we need better strtotime(), but I'm afraid that then we'd need to parse datetime input strings ourselves and use gmmktime() (or similar) to build the timestamp. But that's not necessarily a bad thing? Because then we could use a drop-down list to select the format - and that should be pretty unambiguous. We could even save the format (per user) so that it'll be pre-selected the next time the user gets to select publish/unpublish datetimes. End of whining.
However, I really don't like the idea of using drop-down lists to select year/month/day/etc. It's not usable. If the user needs visual widget to enter datetime, better let the user use jscalendar.
#4
Yeah, six widgets for input of a time is probably a bit excessive ;-)
#5
For your pleasure and entertainment (don't laugh too hard).
Please review and test this thoroughly, since it might break scheduler if it is flawed. We need to be careful with this change anyways.
Meanwhile I will integrate the function into scheduler.module and provide means of configuration for the date format.
/**
* Parse a time/date generated with strftime() as UTC
*
* @param string $date The string to parse
* @param string $format The format used in $date. See strftime() (http://www.php.net/manual/en/function.strftime.php)
* specification for format options. Right now only %d, %H, %I, %m, %M, %p, %S, %y and %Y are supported.
* @return the parsed time as a UTC timestamp
* @see strftime()
*/
function _scheduler_strptime($date, $format) {
# we need to build a regex pattern for the date format
$strftime_entities = array('%d', '%H', '%I', '%m', '%M', '%p', '%S', '%y', '%Y');
$strftime_regex_replacements = array('(\d{2})', '(\d{2})', '(\d{2})', '(\d{2})', '(\d{2})', '(\w{2})', '(\d{2})', '(\d{2})', '(\d{4})');
$custom_pattern = str_replace($strftime_entities, $strftime_regex_replacements, $format);
if (!preg_match("#$custom_pattern#", $date, $value_matches)) {
return FALSE;
}
if (!preg_match_all("/%(\w)/", $format, $entity_matches)) {
return FALSE;
}
$results = array('day' => 0, 'hour' => 0, 'month' => 0, 'minute' => 0, 'meridiem' => NULL, 'second' => 0, 'year' => 0);
$index = 1;
foreach ($entity_matches[1] as $entity) {
$value = intval($value_matches[$index]);
switch ($entity) {
case 'd':
case 'e':
$results['day'] = $value;
break;
case 'H':
case 'I':
$results['hour'] = $value;
break;
case 'm':
$results['month'] = $value;
break;
case 'M':
$results['minute'] = $value;
break;
case 'p':
$results['meridiem'] = $value_matches[$index];
break;
case 'S':
$results['second'] = $value;
break;
case 'y':
case 'Y':
$results['year'] = $value;
break;
}
$index++;
}
if ((strncasecmp($results['meridiem'], "pm", 2) == 0) && ($results['hour'] <= 12)) {
$results['hour'] += 12;
}
$time = gmmktime( $results['hour'], $results['minute'], $results['second'], $results['month'], $results['day'], $results['year'] );
return $time;
}
#6
Go ahead and shoot me. The above is uses strftime() date format but we need date() format options otherwise we end up creating our own format_date() function. Here you go:
/**
* Parse a time/date as UTC time
*
* @param string $date The string to parse
* @param string $format The format used in $date. See date() (http://www.php.net/manual/en/function.date.php)
* specification for format options. Right now only dHhmiaAsyY are supported.
* @return the parsed time as a UTC timestamp
* @see date()
*/
function _scheduler_strptime($date, $format) {
# we need to build a regex pattern for the date format
$strftime_entities = array('d', 'H', 'h', 'm', 'i', 'a', 'A', 's', 'y', 'Y');
$strftime_regex_replacements = array('(\d{2})', '(\d{2})', '(\d{2})', '(\d{2})', '(\d{2})', '(\w{2})', '(\w{2})', '(\d{2})', '(\d{2})', '(\d{4})');
$custom_pattern = str_replace($strftime_entities, $strftime_regex_replacements, $format);
if (!preg_match("#$custom_pattern#", $date, $value_matches)) {
return FALSE;
}
if (!preg_match_all("/(\w)/", $format, $entity_matches)) {
return FALSE;
}
$results = array('day' => 0, 'hour' => 0, 'month' => 0, 'minute' => 0, 'meridiem' => NULL, 'second' => 0, 'year' => 0);
$index = 1;
foreach ($entity_matches[1] as $entity) {
$value = intval($value_matches[$index]);
switch ($entity) {
case 'd':
$results['day'] = $value;
break;
case 'H':
case 'h':
$results['hour'] = $value;
break;
case 'm':
$results['month'] = $value;
break;
case 'i':
$results['minute'] = $value;
break;
case 'a':
case 'A':
$results['meridiem'] = $value_matches[$index];
break;
case 's':
$results['second'] = $value;
break;
case 'y':
case 'Y':
$results['year'] = $value;
break;
}
$index++;
}
if ((strncasecmp($results['meridiem'], "pm", 2) == 0) && ($results['hour'] <= 12)) {
$results['hour'] += 12;
}
$time = gmmktime( $results['hour'], $results['minute'], $results['second'], $results['month'], $results['day'], $results['year'] );
return $time;
}
#7
The attached patch is diff'ed against the current CVS version of scheduler.module (1.46.4.31).
Did you know jscalendar 24-hour mode did not work with scheduler? I fixed it because I had to extend the jscalendar parameters for user defined date format anyways. 12/24-hour mode is now selected by looking at the date format.
#8
http://drupal.org/cvs?commit=117967
#9
Automatically closed -- issue fixed for two weeks with no activity.