source: branches/0.4/web/addons/job_monarch/dwoo/plugins/builtin/functions/date_format.php @ 755

Last change on this file since 755 was 755, checked in by ramonb, 11 years ago
  • add Dwoo
File size: 1.8 KB
Line 
1<?php
2
3/**
4 * Formats a date
5 * <pre>
6 *  * value : the date, as a unix timestamp, mysql datetime or whatever strtotime() can parse
7 *  * format : output format, see {@link http://php.net/strftime} for details
8 *  * default : a default timestamp value, if the first one is empty
9 * </pre>
10 * This software is provided 'as-is', without any express or implied warranty.
11 * In no event will the authors be held liable for any damages arising from the use of this software.
12 *
13 * @author     Jordi Boggiano <j.boggiano@seld.be>
14 * @copyright  Copyright (c) 2008, Jordi Boggiano
15 * @license    http://dwoo.org/LICENSE   Modified BSD License
16 * @link       http://dwoo.org/
17 * @version    1.0.1
18 * @date       2008-12-24
19 * @package    Dwoo
20 */
21function Dwoo_Plugin_date_format(Dwoo_Core $dwoo, $value, $format='%b %e, %Y', $default=null)
22{
23        if (!empty($value)) {
24                // convert if it's not a valid unix timestamp
25                if (preg_match('#^-?\d{1,10}$#', $value)===0) {
26                        $value = strtotime($value);
27                }
28        } elseif (!empty($default)) {
29                // convert if it's not a valid unix timestamp
30                if (preg_match('#^-?\d{1,10}$#', $default)===0) {
31                        $value = strtotime($default);
32                } else {
33                        $value = $default;
34                }
35        } else {
36                return '';
37        }
38
39        // Credits for that windows compat block to Monte Ohrt who made smarty's date_format plugin
40        if (DIRECTORY_SEPARATOR == '\\') {
41                $_win_from = array('%D',       '%h', '%n', '%r',          '%R',    '%t', '%T');
42                $_win_to   = array('%m/%d/%y', '%b', "\n", '%I:%M:%S %p', '%H:%M', "\t", '%H:%M:%S');
43                if (strpos($format, '%e') !== false) {
44                        $_win_from[] = '%e';
45                        $_win_to[]   = sprintf('%\' 2d', date('j', $value));
46                }
47                if (strpos($format, '%l') !== false) {
48                        $_win_from[] = '%l';
49                        $_win_to[]   = sprintf('%\' 2d', date('h', $value));
50                }
51                $format = str_replace($_win_from, $_win_to, $format);
52        }
53        return strftime($format, $value);
54}
Note: See TracBrowser for help on using the repository browser.