forked from NetCommons3/NetCommons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateHelper.php
More file actions
91 lines (79 loc) · 2.15 KB
/
DateHelper.php
File metadata and controls
91 lines (79 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/**
* DateHelper Helper
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Kotaro Hokada <kotaro.hokada@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('AppHelper', 'View/Helper');
App::uses('Validation', 'Utility');
App::uses('NetCommonsTimeHelper', 'NetCommons.View/Helper');
/**
* DateHelper Helper
*
* @author Kotaro Hokada <kotaro.hokada@gmail.com>
* @package NetCommons\NetCommons\View\Helper
*/
class DateHelper extends AppHelper {
/**
* use helpers
*
* @var array
*/
public $helpers = array(
'NetCommons.NetCommonsTime',
'Time',
);
/**
* Date Format
*
* @param datetime $date datetime
* @param null|string $format フォーマット
* @return array
*/
public function dateFormat($date, $format = null) {
if (! Validation::datetime($date)) {
return null;
}
// ユーザタイムゾーンに変換
$date = $this->NetCommonsTime->toUserDatetime($date);
if ($format) {
return date($format, strtotime($date));
} elseif ($this->_isToday($date)) {
return date('G:i', strtotime($date));
} elseif (! $this->_isThisYear($date)) {
return date('Y/m/d', strtotime($date));
} else {
return date('m/d', strtotime($date));
}
}
/**
* ユーザタイムゾーンで今日の日付かの判定
*
* @param string $date user timezone datetime
* @return bool
*/
protected function _isToday($date) {
$now = $this->NetCommonsTime->getNowDatetime();
$nowUserDatetime = $this->NetCommonsTime->toUserDatetime($now);
$dateYmd = date('Y-m-d', strtotime($date));
$nowYmd = date('Y-m-d', strtotime($nowUserDatetime));
return ($dateYmd === $nowYmd);
}
/**
* ユーザタイムゾーンで今年の日時かの判定
*
* @param string $date user timezone datetime
* @return bool
*/
protected function _isThisYear($date) {
$now = $this->NetCommonsTime->getNowDatetime();
$nowUserDatetime = $this->NetCommonsTime->toUserDatetime($now);
$dateYear = date('Y', strtotime($date));
$nowYear = date('Y', strtotime($nowUserDatetime));
return ($dateYear === $nowYear);
}
}