1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2013-2017 |
4
|
|
|
* |
5
|
|
|
* @category Library |
6
|
|
|
* @package Dwoo\Plugins\Functions |
7
|
|
|
* @author Jordi Boggiano <[email protected]> |
8
|
|
|
* @author David Sanchez <[email protected]> |
9
|
|
|
* @copyright 2008-2013 Jordi Boggiano |
10
|
|
|
* @copyright 2013-2017 David Sanchez |
11
|
|
|
* @license http://dwoo.org/LICENSE Modified BSD License |
12
|
|
|
* @version 1.3.2 |
13
|
|
|
* @date 2017-01-06 |
14
|
|
|
* @link http://dwoo.org/ |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace Dwoo\Plugins\Functions; |
18
|
|
|
|
19
|
|
|
use Dwoo\Core; |
20
|
|
|
use Dwoo\Plugin; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Formats a date |
24
|
|
|
* <pre> |
25
|
|
|
* * value : the date, as a unix timestamp, mysql datetime or whatever strtotime() can parse |
26
|
|
|
* * format : output format, see {@link http://php.net/strftime} for details |
27
|
|
|
* * default : a default timestamp value, if the first one is empty |
28
|
|
|
* </pre> |
29
|
|
|
* This software is provided 'as-is', without any express or implied warranty. |
30
|
|
|
* In no event will the authors be held liable for any damages arising from the use of this software. |
31
|
|
|
*/ |
32
|
|
|
class PluginDateFormat extends Plugin |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @param mixed $value |
36
|
|
|
* @param string $format |
37
|
|
|
* @param null $default |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public function process($value, $format = '%b %e, %Y', $default = null) |
42
|
|
|
{ |
43
|
|
|
if (!empty($value)) { |
44
|
|
|
// convert if it's not a valid unix timestamp |
45
|
|
|
if (preg_match('#^-?\d{1,10}$#', $value) === 0) { |
46
|
|
|
$value = strtotime($value); |
47
|
|
|
} |
48
|
|
|
} elseif (!empty($default)) { |
49
|
|
|
// convert if it's not a valid unix timestamp |
50
|
|
|
if (preg_match('#^-?\d{1,10}$#', $default) === 0) { |
51
|
|
|
$value = strtotime($default); |
52
|
|
|
} else { |
53
|
|
|
$value = $default; |
54
|
|
|
} |
55
|
|
|
} else { |
56
|
|
|
return ''; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// Credits for that windows compat block to Monte Ohrt who made smarty's date_format plugin |
60
|
|
|
if (DIRECTORY_SEPARATOR == '\\') { |
61
|
|
|
$_win_from = array( |
62
|
|
|
'%D', |
63
|
|
|
'%h', |
64
|
|
|
'%n', |
65
|
|
|
'%r', |
66
|
|
|
'%R', |
67
|
|
|
'%t', |
68
|
|
|
'%T' |
69
|
|
|
); |
70
|
|
|
$_win_to = array( |
71
|
|
|
'%m/%d/%y', |
72
|
|
|
'%b', |
73
|
|
|
"\n", |
74
|
|
|
'%I:%M:%S %p', |
75
|
|
|
'%H:%M', |
76
|
|
|
"\t", |
77
|
|
|
'%H:%M:%S' |
78
|
|
|
); |
79
|
|
View Code Duplication |
if (strpos($format, '%e') !== false) { |
|
|
|
|
80
|
|
|
$_win_from[] = '%e'; |
81
|
|
|
$_win_to[] = sprintf('%\' 2d', date('j', $value)); |
82
|
|
|
} |
83
|
|
View Code Duplication |
if (strpos($format, '%l') !== false) { |
|
|
|
|
84
|
|
|
$_win_from[] = '%l'; |
85
|
|
|
$_win_to[] = sprintf('%\' 2d', date('h', $value)); |
86
|
|
|
} |
87
|
|
|
$format = str_replace($_win_from, $_win_to, $format); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return strftime($format, $value); |
91
|
|
|
} |
92
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.