2012-07-04 07:23:08 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Smarty plugin
|
2014-09-07 13:38:28 +02:00
|
|
|
*
|
|
|
|
* @package Smarty
|
2012-07-04 07:23:08 +02:00
|
|
|
* @subpackage PluginsModifier
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Smarty date_format modifier plugin
|
|
|
|
* Type: modifier<br>
|
|
|
|
* Name: date_format<br>
|
|
|
|
* Purpose: format datestamps via strftime<br>
|
|
|
|
* Input:<br>
|
|
|
|
* - string: input date string
|
|
|
|
* - format: strftime format for output
|
|
|
|
* - default_date: default date if $string is empty
|
2014-09-07 13:38:28 +02:00
|
|
|
*
|
|
|
|
* @link http://www.smarty.net/manual/en/language.modifier.date.format.php date_format (Smarty online manual)
|
|
|
|
* @author Monte Ohrt <monte at ohrt dot com>
|
|
|
|
*
|
2012-07-04 07:23:08 +02:00
|
|
|
* @param string $string input date string
|
|
|
|
* @param string $format strftime format for output
|
|
|
|
* @param string $default_date default date if $string is empty
|
|
|
|
* @param string $formatter either 'strftime' or 'auto'
|
2014-09-07 13:38:28 +02:00
|
|
|
*
|
2012-07-04 07:23:08 +02:00
|
|
|
* @return string |void
|
2014-09-07 13:38:28 +02:00
|
|
|
* @uses smarty_make_timestamp()
|
2012-07-04 07:23:08 +02:00
|
|
|
*/
|
2014-09-07 13:38:28 +02:00
|
|
|
function smarty_modifier_date_format($string, $format = null, $default_date = '', $formatter = 'auto')
|
2012-07-04 07:23:08 +02:00
|
|
|
{
|
|
|
|
if ($format === null) {
|
|
|
|
$format = Smarty::$_DATE_FORMAT;
|
|
|
|
}
|
|
|
|
/**
|
2014-09-07 13:38:28 +02:00
|
|
|
* Include the {@link shared.make_timestamp.php} plugin
|
|
|
|
*/
|
2012-07-04 07:23:08 +02:00
|
|
|
require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php');
|
|
|
|
if ($string != '' && $string != '0000-00-00' && $string != '0000-00-00 00:00:00') {
|
|
|
|
$timestamp = smarty_make_timestamp($string);
|
|
|
|
} elseif ($default_date != '') {
|
|
|
|
$timestamp = smarty_make_timestamp($default_date);
|
|
|
|
} else {
|
|
|
|
return;
|
2014-09-07 13:38:28 +02:00
|
|
|
}
|
|
|
|
if ($formatter == 'strftime' || ($formatter == 'auto' && strpos($format, '%') !== false)) {
|
2012-07-04 07:23:08 +02:00
|
|
|
if (DS == '\\') {
|
|
|
|
$_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));
|
2014-09-07 13:38:28 +02:00
|
|
|
}
|
2012-07-04 07:23:08 +02:00
|
|
|
if (strpos($format, '%l') !== false) {
|
|
|
|
$_win_from[] = '%l';
|
|
|
|
$_win_to[] = sprintf('%\' 2d', date('h', $timestamp));
|
2014-09-07 13:38:28 +02:00
|
|
|
}
|
2012-07-04 07:23:08 +02:00
|
|
|
$format = str_replace($_win_from, $_win_to, $format);
|
2014-09-07 13:38:28 +02:00
|
|
|
}
|
|
|
|
|
2012-07-04 07:23:08 +02:00
|
|
|
return strftime($format, $timestamp);
|
|
|
|
} else {
|
|
|
|
return date($format, $timestamp);
|
|
|
|
}
|
2014-09-07 13:38:28 +02:00
|
|
|
}
|