1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Provide date or time relate function |
4
|
|
|
* |
5
|
|
|
* @package fwolflib |
6
|
|
|
* @subpackage func.datetime |
7
|
|
|
* @copyright Copyright 2009-2012, Fwolf |
8
|
|
|
* @author Fwolf <[email protected]> |
9
|
|
|
* @since 2009-02-24 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
require_once(dirname(__FILE__) . '/../fwolflib.php'); |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Convert sec back to str describe |
18
|
|
|
* |
19
|
|
|
* No week in result. |
20
|
|
|
* |
21
|
|
|
* @deprecated Use Fwlib\Util\DatetimeUtil::cvtSecToStr() |
22
|
|
|
* @param int $i_sec |
23
|
|
|
* @param boolean $b_simple If true, use ymdhis instead of word |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
|
|
function SecToStr ($i_sec, $b_simple = true) { |
27
|
|
|
if (empty($i_sec) || !is_numeric($i_sec)) |
28
|
|
|
return ''; |
29
|
|
|
|
30
|
|
|
$ar_dict = array( |
31
|
|
|
array('c', -1, 'century', 'centuries'), |
32
|
|
|
array('y', 100, 'year', 'years'), |
33
|
|
|
// 12m != 1y, can't count month in. |
34
|
|
|
// array('m', 12, 'month', 'months'), |
35
|
|
|
array('d', 365, 'day', 'days'), |
36
|
|
|
array('h', 24, 'hour', 'hours'), |
37
|
|
|
array('i', 60, 'minute', 'minutes'), |
38
|
|
|
array('s', 60, 'second', 'seconds'), |
39
|
|
|
); |
40
|
|
|
$i = count($ar_dict); |
41
|
|
|
// Loop from end of $ar_dict |
42
|
|
|
$s = ''; |
43
|
|
|
while (0 < $i && 0 < $i_sec) { |
44
|
|
|
// 1. for loop, 2. got current array index |
45
|
|
|
$i --; |
46
|
|
|
|
47
|
|
|
// Reach top level, end loop |
48
|
|
|
if (-1 == $ar_dict[$i][1]) { |
49
|
|
|
$s = $i_sec . $ar_dict[$i][(($b_simple) ? 0 |
50
|
|
|
: ((1 == $i_sec) ? 2 : 3))] |
51
|
|
|
. ' ' . $s; |
52
|
|
|
break; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$j = $i_sec % $ar_dict[$i][1]; |
56
|
|
|
if (0 != $j) |
57
|
|
|
$s = $j . $ar_dict[$i][(($b_simple) ? 0 |
58
|
|
|
: ((1 == $i_sec) ? 2 : 3))] |
59
|
|
|
. ' ' . $s; |
60
|
|
|
$i_sec = floor($i_sec / $ar_dict[$i][1]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return rtrim($s); |
64
|
|
|
} // end of func SecToStr |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Convert str to seconds it means |
69
|
|
|
* |
70
|
|
|
* Like 1m, 20d or combined |
71
|
|
|
* |
72
|
|
|
* Solid: 1m = 30d, 1y = 365d |
73
|
|
|
* |
74
|
|
|
* @deprecated Use Fwlib\Util\DatetimeUtil::cvtStrToSec() |
75
|
|
|
* @param string $str |
76
|
|
|
* @return integer |
77
|
|
|
*/ |
78
|
|
|
function StrToSec ($str) { |
79
|
|
|
if (empty($str)) |
80
|
|
|
return 0; |
81
|
|
|
|
82
|
|
|
// All number, return directly |
83
|
|
|
if (is_numeric($str)) |
84
|
|
|
return $str; |
85
|
|
|
|
86
|
|
|
// Parse c, y, m, w, d, h, i, s |
87
|
|
|
$str = strtolower($str); |
88
|
|
|
$str = strtr($str, array( |
89
|
|
|
'sec' => 's', |
90
|
|
|
'second' => 's', |
91
|
|
|
'seconds' => 's', |
92
|
|
|
'min' => 'i', |
93
|
|
|
'minute' => 'i', |
94
|
|
|
'minutes' => 'i', |
95
|
|
|
'hour' => 'h', |
96
|
|
|
'hours' => 'h', |
97
|
|
|
'day' => 'd', |
98
|
|
|
'days' => 'd', |
99
|
|
|
'week' => 'w', |
100
|
|
|
'weeks' => 'w', |
101
|
|
|
'month' => 'm', |
102
|
|
|
'months' => 'm', |
103
|
|
|
'year' => 'y', |
104
|
|
|
'years' => 'y', |
105
|
|
|
'century' => 'c', |
106
|
|
|
'centuries' => 'c', |
107
|
|
|
)); |
108
|
|
|
$str = preg_replace(array( |
109
|
|
|
'/([+-]?\d+)s/', |
110
|
|
|
'/([+-]?\d+)i/', |
111
|
|
|
'/([+-]?\d+)h/', |
112
|
|
|
'/([+-]?\d+)d/', |
113
|
|
|
'/([+-]?\d+)w/', |
114
|
|
|
'/([+-]?\d+)m/', |
115
|
|
|
'/([+-]?\d+)y/', |
116
|
|
|
'/([+-]?\d+)c/', |
117
|
|
|
), array( |
118
|
|
|
'+$1 ', |
119
|
|
|
'+$1 * 60 ', |
120
|
|
|
'+$1 * 3600 ', |
121
|
|
|
'+$1 * 86400 ', |
122
|
|
|
'+$1 * 604800 ', |
123
|
|
|
'+$1 * 2592000 ', |
124
|
|
|
'+$1 * 31536000 ', |
125
|
|
|
'+$1 * 3153600000 ', |
126
|
|
|
), $str); |
127
|
|
|
// Fix +- |
128
|
|
|
$str = preg_replace('/\+\s*\-/', '-', $str); |
129
|
|
|
$str = preg_replace('/\-\s*\+/', '-', $str); |
130
|
|
|
$str = preg_replace('/\+\s*\+/', '+', $str); |
131
|
|
|
eval('$i_sec = ' . $str . ';'); |
132
|
|
|
return $i_sec; |
133
|
|
|
} // end of func StrToSec |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* strtotime added remove of ':000' in sybase time(probably because dblib) |
138
|
|
|
* |
139
|
|
|
* @deprecated Use Fwlib\Util\DatetimeUtil::cvtTimeFromSybase() |
140
|
|
|
* @param string $time |
141
|
|
|
* @return integer |
142
|
|
|
*/ |
143
|
|
|
function Strtotime1 ($time) { |
144
|
|
|
if (!empty($time)) { |
145
|
|
|
$time = preg_replace('/:\d{3}/', '', $time); |
146
|
|
|
} |
147
|
|
|
return strtotime($time); |
148
|
|
|
} // end of func Strtotime1 |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
?> |
|
|
|
|
152
|
|
|
|
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.
A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.