|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Date |
|
4
|
|
|
* |
|
5
|
|
|
* 日付操作のためのファンクション群 |
|
6
|
|
|
* |
|
7
|
|
|
* @package risoluto |
|
8
|
|
|
* @author Risoluto Developers |
|
9
|
|
|
* @license http://opensource.org/licenses/bsd-license.php new BSD license |
|
10
|
|
|
* @copyright (C) 2008-2015 Risoluto Developers / All Rights Reserved. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
//------------------------------------------------------// |
|
14
|
|
|
// 名前空間の定義 |
|
15
|
|
|
//------------------------------------------------------// |
|
16
|
|
|
namespace Risoluto; |
|
17
|
|
|
|
|
18
|
|
|
class Date |
|
19
|
|
|
{ |
|
20
|
|
|
//------------------------------------------------------// |
|
21
|
|
|
// クラスメソッド定義 |
|
22
|
|
|
//------------------------------------------------------// |
|
23
|
|
|
/** |
|
24
|
|
|
* __construct() |
|
25
|
|
|
* |
|
26
|
|
|
* コンストラクタ |
|
27
|
|
|
*/ |
|
28
|
|
|
private function __construct() |
|
29
|
|
|
{ |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* cnvYear($year) |
|
34
|
|
|
* |
|
35
|
|
|
* 西暦に対応する和暦を取得する |
|
36
|
|
|
* |
|
37
|
|
|
* @access public |
|
38
|
|
|
* |
|
39
|
|
|
* @param integer $year 対象となる西暦年(4桁) |
|
40
|
|
|
* |
|
41
|
|
|
* @return string 引数に指定された西暦年に対応する和暦年(変換に失敗した場合は空文字を返却) |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function cnvYear( $year ) |
|
44
|
|
|
{ |
|
45
|
|
|
// 引数に指定された値が、1868年より以前か数字4桁でない場合は空文字を返却する |
|
46
|
|
|
if (!is_numeric( $year ) or strlen( $year ) != 4 or $year < 1868) { |
|
47
|
|
|
return ''; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// 明治(1868年1月25日〜1912年7月29日、明治45年まで) |
|
51
|
|
|
if (Validate::isBetween( $year, 1868, 1912 )) { |
|
52
|
|
|
// 算出する |
|
53
|
|
|
$retval = self::genEraName( ( $year - 1868 ) + 1, '明治', '大正', 45 ); |
|
54
|
|
|
|
|
55
|
|
|
// 大正(1912年7月30日〜1926年12月24日、大正15年まで) |
|
56
|
|
|
} elseif (Validate::isBetween( $year, 1912, 1926 )) { |
|
57
|
|
|
// 算出する |
|
58
|
|
|
$retval = self::genEraName( ( $year - 1912 ) + 1, '大正', '昭和', 15 ); |
|
59
|
|
|
|
|
60
|
|
|
// 昭和(1926年12月25日〜1989年1月7日、昭和64年まで) |
|
61
|
|
|
} elseif (Validate::isBetween( $year, 1926, 1989 )) { |
|
62
|
|
|
// 算出する |
|
63
|
|
|
$retval = self::genEraName( ( $year - 1926 ) + 1, '昭和', '平成', 64 ); |
|
64
|
|
|
|
|
65
|
|
|
// 平成(1989年1月8日〜) |
|
66
|
|
|
} else { |
|
67
|
|
|
// 算出する |
|
68
|
|
|
$retval = self::genEraName( ( $year - 1989 ) + 1, '平成' ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $retval; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* genEraName($year, $currentEraName, $nextEraName = '', $borderYear = '') |
|
76
|
|
|
* |
|
77
|
|
|
* @access private |
|
78
|
|
|
* |
|
79
|
|
|
* @param integer $year 年 |
|
80
|
|
|
* @param string $currentEraName 和暦年号 |
|
81
|
|
|
* @param string $nextEraName 次の和暦年号 |
|
82
|
|
|
* @param string $borderYear 次の和暦年号との境界年 |
|
83
|
|
|
* |
|
84
|
|
|
* @return string 生成された和暦年号表記 |
|
85
|
|
|
*/ |
|
86
|
|
|
private static function genEraName( $year, $currentEraName, $nextEraName = '', $borderYear = '' ) |
|
87
|
|
|
{ |
|
88
|
|
|
// 1年の場合は元年として表示する |
|
89
|
|
|
if ($year == '1') { |
|
90
|
|
|
$retval = $currentEraName . '元年'; |
|
91
|
|
|
} else { |
|
92
|
|
|
// 年号を生成する |
|
93
|
|
|
$retval = $currentEraName . $year . '年'; |
|
94
|
|
|
|
|
95
|
|
|
// 境界年の場合は、両方の年号を併記する |
|
96
|
|
|
if (!empty( $nextEraName ) and !empty( $borderYear ) and $year == $borderYear) { |
|
97
|
|
|
$retval .= ' / ' . $nextEraName . '元年'; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $retval; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* genYear($firstType = false, $nonSelectedVal = '', $nonSelectedStr = '', $base = null, $limit = null, $mode = 0) |
|
106
|
|
|
* |
|
107
|
|
|
* 「年」の情報が格納された配列を生成する |
|
108
|
|
|
* |
|
109
|
|
|
* @access public |
|
110
|
|
|
* |
|
111
|
|
|
* @param boolean $firstType 配列の先頭に未選択状態を示す要素をセットするか(デフォルト:false) |
|
112
|
|
|
* @param string $nonSelectedVal 未選択状態を示す要素の要素値(デフォルト:'') |
|
113
|
|
|
* @param string $nonSelectedStr 未選択状態を示す要素の配列値(デフォルト:'') |
|
114
|
|
|
* @param integer $base 生成開始年(西暦指定、デフォルトは現在年 - 5) |
|
115
|
|
|
* @param integer $limit 生成年数(デフォルトは10) |
|
116
|
|
|
* @param integer $mode 返却する配列のタイプ(デフォルト西暦のみ、0:西暦のみ/1:和暦のみ/2:両方) |
|
117
|
|
|
* |
|
118
|
|
|
* @return array 「年」の情報が格納された配列 |
|
119
|
|
|
*/ |
|
120
|
|
|
public static function genYear( |
|
121
|
|
|
$firstType = false, |
|
122
|
|
|
$nonSelectedVal = '', |
|
123
|
|
|
$nonSelectedStr = '', |
|
124
|
|
|
$base = null, |
|
125
|
|
|
$limit = null, |
|
126
|
|
|
$mode = 0 |
|
127
|
|
|
) { |
|
128
|
|
|
// 配列を初期化 |
|
129
|
|
|
$retVal = [ ]; |
|
130
|
|
|
if ($firstType) { |
|
131
|
|
|
$retVal[ $nonSelectedVal ] = $nonSelectedStr; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
// 生成開始年の設定 |
|
135
|
|
|
$beginYear = date( 'Y' ) - 5; |
|
136
|
|
|
if (is_numeric( $base ) and strlen( $base ) == 4) { |
|
137
|
|
|
$beginYear = $base; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
// 生成年数の設定 |
|
141
|
|
|
$endYearCnt = 10; |
|
142
|
|
|
if (is_numeric( $limit ) and $limit > 0) { |
|
143
|
|
|
$endYearCnt = $limit; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
// 引数で指定された配列のタイプに合わせて年のリストを作成する |
|
147
|
|
|
for ($cnt = 0; $cnt < $endYearCnt; $cnt++) { |
|
148
|
|
|
//--- 処理中の年 |
|
149
|
|
|
$currentYear = $beginYear + $cnt; |
|
150
|
|
|
|
|
151
|
|
|
//--- 「和暦のみ」以外の時は西暦表示を生成 |
|
152
|
|
|
$tmp_AD = ''; |
|
153
|
|
|
if ($mode != '1') { |
|
154
|
|
|
$tmp_AD = sprintf( "%04d", $currentYear ); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
//--- 「西暦のみ」以外の時は和暦表示を生成 |
|
158
|
|
|
$tmp_JIY = ''; |
|
159
|
|
|
if ($mode != '0') { |
|
160
|
|
|
$tmp_JIY = self::CnvYear( sprintf( "%04d", $currentYear ) ); |
|
161
|
|
|
// 「両方」の場合は括弧でくくる |
|
162
|
|
|
$tmp_JIY = '(' . $tmp_JIY . ')'; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
// 配列に追加する |
|
166
|
|
|
$retVal[ $currentYear ] = $tmp_AD . $tmp_JIY; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $retVal; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* genMonth($firstType = false, $nonSelectedVal = '', $nonSelectedStr = '') |
|
174
|
|
|
* |
|
175
|
|
|
* 「月」の情報が格納された配列を生成する |
|
176
|
|
|
* |
|
177
|
|
|
* @access public |
|
178
|
|
|
* |
|
179
|
|
|
* @param boolean $firstType 配列の先頭に未選択状態を示す要素をセットするか(デフォルト:false) |
|
180
|
|
|
* @param string $nonSelectedVal 未選択状態を示す要素の要素値(デフォルト:'') |
|
181
|
|
|
* @param string $nonSelectedStr 未選択状態を示す要素の配列値(デフォルト:'') |
|
182
|
|
|
* |
|
183
|
|
|
* @return array 「月」の情報が格納された配列 |
|
184
|
|
|
*/ |
|
185
|
|
|
public static function genMonth( $firstType = false, $nonSelectedVal = '', $nonSelectedStr = '' ) |
|
186
|
|
|
{ |
|
187
|
|
|
// 配列を生成する |
|
188
|
|
|
$retVal = self::genNumberList( 12, $firstType, $nonSelectedVal, $nonSelectedStr ); |
|
189
|
|
|
|
|
190
|
|
|
return $retVal; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* genDay($firstType = false, $nonSelectedVal = '', $nonSelectedStr = '', $target = array('month' => '', 'year' => |
|
195
|
|
|
* '')) |
|
196
|
|
|
* |
|
197
|
|
|
* 「日」の情報が格納された配列を生成する |
|
198
|
|
|
* |
|
199
|
|
|
* @access public |
|
200
|
|
|
* |
|
201
|
|
|
* @param boolean $firstType 配列の先頭に未選択状態を示す要素をセットするか(デフォルト:false) |
|
202
|
|
|
* @param string $nonSelectedVal 未選択状態を示す要素の要素値(デフォルト:'') |
|
203
|
|
|
* @param string $nonSelectedStr 未選択状態を示す要素の配列値(デフォルト:'') |
|
204
|
|
|
* @param array $target 生成対象となる月と年の連想配列(デフォルト:array('month' => '', year => |
|
205
|
|
|
* '')、monthを指定するとその月の日数に基づいた内容が返却され、monthが"2"の場合でyearがセットされていると閏年判定を行う) |
|
206
|
|
|
* |
|
207
|
|
|
* @return array 「日」の情報が格納された配列 |
|
208
|
|
|
*/ |
|
209
|
|
|
public static function genDay( |
|
210
|
|
|
$firstType = false, |
|
211
|
|
|
$nonSelectedVal = '', |
|
212
|
|
|
$nonSelectedStr = '', |
|
213
|
|
|
$target = [ 'month' => '', 'year' => '' ] |
|
214
|
|
|
) { |
|
215
|
|
|
// 配列を初期化 |
|
216
|
|
|
$retVal = [ ]; |
|
217
|
|
|
if ($firstType) { |
|
218
|
|
|
$retVal[ $nonSelectedVal ] = $nonSelectedStr; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
// 月ごとに日数を決定する |
|
222
|
|
|
$endDay = date( "t", mktime( 0, 0, 0, ( !empty( $target[ 'month' ] ) ? $target[ 'month' ] : 0 ), 1, |
|
223
|
|
|
( !empty( $target[ 'year' ] ) ? $target[ 'year' ] : 0 ) ) ); |
|
224
|
|
|
|
|
225
|
|
|
// 配列を生成する |
|
226
|
|
|
$retVal = self::genNumberList( $endDay, $firstType, $nonSelectedVal, $nonSelectedStr ); |
|
227
|
|
|
|
|
228
|
|
|
return $retVal; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* genHour($firstType = false, $nonSelectedVal = '', $nonSelectedStr = '', $hourType = true) |
|
233
|
|
|
* |
|
234
|
|
|
* 「時」の情報が格納された配列を生成する |
|
235
|
|
|
* |
|
236
|
|
|
* @access public |
|
237
|
|
|
* |
|
238
|
|
|
* @param boolean $firstType 配列の先頭に未選択状態を示す要素をセットするか(デフォルト:false) |
|
239
|
|
|
* @param string $nonSelectedVal 未選択状態を示す要素の要素値(デフォルト:'') |
|
240
|
|
|
* @param string $nonSelectedStr 未選択状態を示す要素の配列値(デフォルト:'') |
|
241
|
|
|
* @param boolean $hourType 表示を24時制にするかどうか(デフォルトtrue、true:24時制/false:12時制、数字の前に「午前」または「午後」がつく) |
|
242
|
|
|
* |
|
243
|
|
|
* @return array 「時」の情報が格納された配列 |
|
244
|
|
|
*/ |
|
245
|
|
|
public static function genHour( $firstType = false, $nonSelectedVal = '', $nonSelectedStr = '', $hourType = true ) |
|
246
|
|
|
{ |
|
247
|
|
|
// 24時間表記の時はそのまま生成 |
|
248
|
|
|
$retVal = self::genNumberList( 23, $firstType, $nonSelectedVal, $nonSelectedStr, 0 ); |
|
249
|
|
|
|
|
250
|
|
|
//12時間表記の時は午前/午後表記に変更する |
|
251
|
|
|
if (!$hourType) { |
|
252
|
|
|
foreach ($retVal as $key => $val) { |
|
253
|
|
|
if ($key != $nonSelectedVal) { |
|
254
|
|
|
$retVal[ $key ] = ( ( $key <= 11 ) ? sprintf( "午前%02d", $val ) : sprintf( "午後%02d", $val - 12 ) ); |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
return $retVal; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* genMinSec($firstType = false, $nonSelectedVal = '', $nonSelectedStr = '') |
|
264
|
|
|
* |
|
265
|
|
|
* 「分」または「秒」の情報が格納された配列を生成する |
|
266
|
|
|
* |
|
267
|
|
|
* @access public |
|
268
|
|
|
* |
|
269
|
|
|
* @param boolean $firstType 配列の先頭に未選択状態を示す要素をセットするか(デフォルト:false) |
|
270
|
|
|
* @param string $nonSelectedVal 未選択状態を示す要素の要素値(デフォルト:'') |
|
271
|
|
|
* @param string $nonSelectedStr 未選択状態を示す要素の配列値(デフォルト:'') |
|
272
|
|
|
* |
|
273
|
|
|
* @return array 「分」の情報が格納された配列 |
|
274
|
|
|
*/ |
|
275
|
|
|
public static function genMinSec( $firstType = false, $nonSelectedVal = '', $nonSelectedStr = '' ) |
|
276
|
|
|
{ |
|
277
|
|
|
// 配列を生成する |
|
278
|
|
|
$retVal = self::genNumberList( 59, $firstType, $nonSelectedVal, $nonSelectedStr, 0 ); |
|
279
|
|
|
|
|
280
|
|
|
return $retVal; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* genNumberList($limit, $firstType = false, $nonSelectedVal = '', $nonSelectedStr = '', $start = 1, $format = |
|
285
|
|
|
* '%02d') |
|
286
|
|
|
* |
|
287
|
|
|
* 月、日、時、分、秒用の数字のリストを生成する |
|
288
|
|
|
* |
|
289
|
|
|
* @access private |
|
290
|
|
|
* |
|
291
|
|
|
* @param integer $limit 生成する最大数 |
|
292
|
|
|
* @param boolean $firstType 配列の先頭に未選択状態を示す要素をセットするか(デフォルト:false) |
|
293
|
|
|
* @param string $nonSelectedVal 未選択状態を示す要素の要素値(デフォルト:'') |
|
294
|
|
|
* @param string $nonSelectedStr 未選択状態を示す要素の配列値(デフォルト:'') |
|
295
|
|
|
* @param integer $start 生成する数字の最小値(デフォルト:1) |
|
296
|
|
|
* @param string $format 生成する数字のフォーマット(デフォルト:'$02d') |
|
297
|
|
|
* |
|
298
|
|
|
* @return array 数値の配列 |
|
299
|
|
|
*/ |
|
300
|
|
|
private static function genNumberList( |
|
301
|
|
|
$limit, |
|
302
|
|
|
$firstType = false, |
|
303
|
|
|
$nonSelectedVal = '', |
|
304
|
|
|
$nonSelectedStr = '', |
|
305
|
|
|
$start = 1, |
|
306
|
|
|
$format = '%02d' |
|
307
|
|
|
) { |
|
308
|
|
|
// 引数に応じて配列の先頭を制御 |
|
309
|
|
|
$retVal = [ ]; |
|
310
|
|
|
if ($firstType) { |
|
311
|
|
|
$retVal[ $nonSelectedVal ] = $nonSelectedStr; |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
// 配列を生成する |
|
315
|
|
|
for ($cnt = $start; $cnt <= $limit; $cnt++) { |
|
316
|
|
|
$tmpVal = sprintf( $format, $cnt ); |
|
317
|
|
|
$retVal[ $tmpVal ] = $tmpVal; |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
return $retVal; |
|
321
|
|
|
} |
|
322
|
|
|
} |