]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/libs/plugins/modifier.date_format.php
Merge pull request #4229 from annando/database
[friendica.git] / vendor / smarty / smarty / libs / plugins / modifier.date_format.php
1 <?php
2 /**
3  * Smarty plugin
4  *
5  * @package    Smarty
6  * @subpackage PluginsModifier
7  */
8
9 /**
10  * Smarty date_format modifier plugin
11  * Type:     modifier<br>
12  * Name:     date_format<br>
13  * Purpose:  format datestamps via strftime<br>
14  * Input:<br>
15  *          - string: input date string
16  *          - format: strftime format for output
17  *          - default_date: default date if $string is empty
18  *
19  * @link   http://www.smarty.net/manual/en/language.modifier.date.format.php date_format (Smarty online manual)
20  * @author Monte Ohrt <monte at ohrt dot com>
21  *
22  * @param string $string       input date string
23  * @param string $format       strftime format for output
24  * @param string $default_date default date if $string is empty
25  * @param string $formatter    either 'strftime' or 'auto'
26  *
27  * @return string |void
28  * @uses   smarty_make_timestamp()
29  */
30 function smarty_modifier_date_format($string, $format = null, $default_date = '', $formatter = 'auto')
31 {
32     if ($format === null) {
33         $format = Smarty::$_DATE_FORMAT;
34     }
35     /**
36      * require_once the {@link shared.make_timestamp.php} plugin
37      */
38     if (!is_callable('smarty_make_timestamp')) {
39         require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php');
40     }
41     if ($string != '' && $string != '0000-00-00' && $string != '0000-00-00 00:00:00') {
42         $timestamp = smarty_make_timestamp($string);
43     } elseif ($default_date != '') {
44         $timestamp = smarty_make_timestamp($default_date);
45     } else {
46         return;
47     }
48     if ($formatter == 'strftime' || ($formatter == 'auto' && strpos($format, '%') !== false)) {
49         if (Smarty::$_IS_WINDOWS) {
50             $_win_from = array('%D', '%h', '%n', '%r', '%R', '%t', '%T');
51             $_win_to = array('%m/%d/%y', '%b', "\n", '%I:%M:%S %p', '%H:%M', "\t", '%H:%M:%S');
52             if (strpos($format, '%e') !== false) {
53                 $_win_from[] = '%e';
54                 $_win_to[] = sprintf('%\' 2d', date('j', $timestamp));
55             }
56             if (strpos($format, '%l') !== false) {
57                 $_win_from[] = '%l';
58                 $_win_to[] = sprintf('%\' 2d', date('h', $timestamp));
59             }
60             $format = str_replace($_win_from, $_win_to, $format);
61         }
62
63         return strftime($format, $timestamp);
64     } else {
65         return date($format, $timestamp);
66     }
67 }