this commit addresses some locale issues (date combobox in search template) and refactors a bit the date_formatting code; date_strformat was not used, so it's been modified to work as it should by moving around things a bit, and making it compatible - for what concerns the prototype of the function - to strftime

This commit is contained in:
real_nowhereman 2008-02-28 14:56:52 +00:00
parent 5c096dcd37
commit 7fc51f79d1
3 changed files with 68 additions and 85 deletions

View File

@ -1,21 +1,49 @@
<?php <?php
function date_strformat($timestamp=null, $offset=0, $format='l dS F Y h:i:s A') { function date_strformat($format, $timestamp=0) {
if ( strpos( $timestamp, ',' ) !== false ) { global $lang;
// This is an hack for compatibility with the time
// format from versions < 0.3.3. In 0.3.3 spb switched // D l day
// to the unix timestamp for storing times.
// if ( strpos($format, '%a') !== false ) {
// Before that it was in this format: $i = strftime('%w', $timestamp);
// date( 'F j, Y, g:i a', $time_stamp ); $format = str_replace('%a', $lang['date']['weekday_abbr'][$i], $format);
// 'May 10, 2004, 3:57 pm'
$time_stamp = str_replace( ',', '', $time_stamp );
$time_stamp = strtotime( $time_stamp );
} }
$timestamp = ($timestamp != null) ? $timestamp : time(); if ( strpos($format, '%A') !== false ) {
$time_stamp = intval($timestamp) + intval($offset) * 60 * 60; $i = strftime('%w', $timestamp);
return date($format, $time_stamp); $format = str_replace('%A', $lang['date']['weekday'][$i], $format);
}
// F M month
if ( strpos($format, '%b') !== false ) {
$i = intval(strftime('%m', $timestamp))-1;
$format = str_replace('%b', $lang['date']['month_abbr'][$i], $format);
}
if ( strpos($format, '%B') !== false ) {
$i = intval(strftime('%m', $timestamp))-1;
$format = str_replace('%B', $lang['date']['month'][$i], $format);
}
if (DIRECTORY_SEPARATOR == '\\') {
$_win_from = array('%D', '%h', '%n', '%r', '%R', '%t', '%T');
$_win_to = array('%m/%d/%y', '%b', "\n", '%I:%M:%S %p', '%H:%M', "\t", '%H:%M:%S');
if (strpos($format, '%e') !== false) {
$_win_from[] = '%e';
$_win_to[] = sprintf('%\' 2d', date('j', $timestamp));
}
if (strpos($format, '%l') !== false) {
$_win_from[] = '%l';
$_win_to[] = sprintf('%\' 2d', date('h', $timestamp));
}
$format = str_replace($_win_from, $_win_to, $format);
}
return strftime($format, $timestamp);
} }

View File

@ -327,13 +327,11 @@
} }
function theme_date_format( $string, function theme_date_format($string, $format = null, $default_date = '') {
$format = null,
$default_date = ''
) {
global $lang;
if ($string != '') { $timestamp = 0;
if ($string) {
$timestamp = $string; // smarty_make_timestamp($string); $timestamp = $string; // smarty_make_timestamp($string);
} elseif ($default_date != '') { } elseif ($default_date != '') {
$timestamp = $default_date; // smarty_make_timestamp($default_date); $timestamp = $default_date; // smarty_make_timestamp($default_date);
@ -347,57 +345,14 @@
} }
// D l day
if ( strpos($format, '%a') !== false ) {
$i = strftime('%w', $timestamp);
$format = str_replace('%a', $lang['date']['weekday_abbr'][$i], $format);
}
if ( strpos($format, '%A') !== false ) {
$i = strftime('%w', $timestamp);
$format = str_replace('%A', $lang['date']['weekday'][$i], $format);
}
// F M month
if ( strpos($format, '%b') !== false ) {
$i = intval(strftime('%m', $timestamp))-1;
$format = str_replace('%b', $lang['date']['month_abbr'][$i], $format);
}
if ( strpos($format, '%B') !== false ) {
$i = intval(strftime('%m', $timestamp))-1;
$format = str_replace('%B', $lang['date']['month'][$i], $format);
}
if (DIRECTORY_SEPARATOR == '\\') {
$_win_from = array('%D', '%h', '%n', '%r', '%R', '%t', '%T');
$_win_to = array('%m/%d/%y', '%b', "\n", '%I:%M:%S %p', '%H:%M', "\t", '%H:%M:%S');
if (strpos($format, '%e') !== false) {
$_win_from[] = '%e';
$_win_to[] = sprintf('%\' 2d', date('j', $timestamp));
}
if (strpos($format, '%l') !== false) {
$_win_from[] = '%l';
$_win_to[] = sprintf('%\' 2d', date('h', $timestamp));
}
$format = str_replace($_win_from, $_win_to, $format);
}
return strftime($format, $timestamp);
return date_strformat($format, $timestamp);
} }
function theme_smarty_modifier_date_format_daily( function theme_smarty_modifier_date_format_daily(
$string, $string, $format = null, $default_date = '' ) {
$format = null,
$default_date = ''
) {
global $THEME_CURRENT_DAY, $lang, $fp_config; global $THEME_CURRENT_DAY, $lang, $fp_config;

View File

@ -42,7 +42,7 @@ function smarty_function_html_select_date($params, &$smarty)
require_once $smarty->_get_plugin_filepath('function','html_options'); require_once $smarty->_get_plugin_filepath('function','html_options');
/* Default values. */ /* Default values. */
$prefix = "Date_"; $prefix = "Date_";
$start_year = strftime("%Y"); $start_year = date_strformat("%Y");
$end_year = $start_year; $end_year = $start_year;
$display_days = true; $display_days = true;
$display_months = true; $display_months = true;
@ -142,8 +142,8 @@ function smarty_function_html_select_date($params, &$smarty)
$time = $found[1]; $time = $found[1];
} else { } else {
// use smarty_make_timestamp to get an unix timestamp and // use smarty_make_timestamp to get an unix timestamp and
// strftime to make yyyy-mm-dd // date_strformat to make yyyy-mm-dd
$time = strftime('%Y-%m-%d', smarty_make_timestamp($time)); $time = date_strformat('%Y-%m-%d', smarty_make_timestamp($time));
} }
// Now split this in pieces, which later can be used to set the select // Now split this in pieces, which later can be used to set the select
$time = explode("-", $time); $time = explode("-", $time);
@ -151,16 +151,16 @@ function smarty_function_html_select_date($params, &$smarty)
// make syntax "+N" or "-N" work with start_year and end_year // make syntax "+N" or "-N" work with start_year and end_year
if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) { if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) {
if ($match[1] == '+') { if ($match[1] == '+') {
$end_year = strftime('%Y') + $match[2]; $end_year = date_strformat('%Y') + $match[2];
} else { } else {
$end_year = strftime('%Y') - $match[2]; $end_year = date_strformat('%Y') - $match[2];
} }
} }
if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) { if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) {
if ($match[1] == '+') { if ($match[1] == '+') {
$start_year = strftime('%Y') + $match[2]; $start_year = date_strformat('%Y') + $match[2];
} else { } else {
$start_year = strftime('%Y') - $match[2]; $start_year = date_strformat('%Y') - $match[2];
} }
} }
if (strlen($time[0]) > 0) { if (strlen($time[0]) > 0) {
@ -188,8 +188,8 @@ function smarty_function_html_select_date($params, &$smarty)
$month_values[''] = ''; $month_values[''] = '';
} }
for ($i = 1; $i <= 12; $i++) { for ($i = 1; $i <= 12; $i++) {
$month_names[$i] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000)); $month_names[$i] = date_strformat($month_format, mktime(0, 0, 0, $i, 1, 2000));
$month_values[$i] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000)); $month_values[$i] = date_strformat($month_value_format, mktime(0, 0, 0, $i, 1, 2000));
} }
$month_result .= '<select name='; $month_result .= '<select name=';
@ -211,7 +211,7 @@ function smarty_function_html_select_date($params, &$smarty)
$month_result .= smarty_function_html_options(array('output' => $month_names, $month_result .= smarty_function_html_options(array('output' => $month_names,
'values' => $month_values, 'values' => $month_values,
'selected' => (int)$time[1] ? strftime($month_value_format, mktime(0, 0, 0, (int)$time[1], 1, 2000)) : '', 'selected' => (int)$time[1] ? date_strformat($month_value_format, mktime(0, 0, 0, (int)$time[1], 1, 2000)) : '',
'print_result' => false), 'print_result' => false),
$smarty); $smarty);
$month_result .= '</select>'; $month_result .= '</select>';