-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateFormat.php
More file actions
149 lines (131 loc) · 3.61 KB
/
DateFormat.php
File metadata and controls
149 lines (131 loc) · 3.61 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace baricio\util;
/**
* Class to retrieve the easy way of date format only with methods call
*
* @author baricio@gmail.com
*/
class DateFormat {
//Current formats to date
public static $FORMAT_DATE = "m/d/Y";
public static $FORMAT_DATETIME = "m/d/Y H:i:s";
public static $FORMAT_BR_DATE = "d/m/Y";
public static $FORMAT_BR_DATETIME = "d/m/Y H:i:s";
public static $FORMAT_MYSQL_DATE = "Y-m-d";
public static $FORMAT_MYSQL_DATETIME = "Y-m-d H:i:s";
//Instance of DateTime PHP Class
private $date;
/**
* Create new instance of DateTime PHP Class
*/
public function __construct() {
$this->date = new \DateTime('now');
}
/**
* Create new instance of DateTime PHP Class with format and time
* @param string $format DateFormat static property
* @param strine $time timestamp
* @return \self
*/
public static function createFromFormat($format, $time) {
$instance = new self();
$instance->date = \DateTime::createFromFormat($format, $time);
return $instance;
}
/**
* Add days to current datetime
* @param int $days
* @return \baricio\util\DateFormat
*/
public function addDay($days){
$this->date->add(new \DateInterval('P'. $days .'D'));
return $this;
}
/**
* Add months to current datetime
* @param int $months
* @return \baricio\util\DateFormat
*/
public function addMonth($months){
$this->date->add(new \DateInterval('P'. $months .'M'));
return $this;
}
/**
* Add years to current datetime
* @param int $years
* @return \baricio\util\DateFormat
*/
public function addYear($years){
$this->date->add(new \DateInterval('P'. $years .'Y'));
return $this;
}
/**
* Add hours to current datetime
* @param int $hours
* @return \baricio\util\DateFormat
*/
public function addHour($hours){
$this->date->add(new \DateInterval('PT'. $hours .'H'));
return $this;
}
/**
* Add minutes to current datetime
* @param int $minutes
* @return \baricio\util\DateFormat
*/
public function addMinute($minutes){
$this->date->add(new \DateInterval('PT'. $minutes .'M'));
return $this;
}
/**
* Add seconds to current datetime
* @param int $seconds
* @return \baricio\util\DateFormat
*/
public function addSecond($seconds){
$this->date->add(new \DateInterval('PT'. $seconds .'S'));
return $this;
}
/**
* Return date m/d/Y
* @return string date
*/
public function getDate() {
return $this->date->format(self::$FORMAT_DATE);
}
/**
* Return date m/d/Y H:i:s
* @return string date
*/
public function getDateTime() {
return $this->date->format(self::$FORMAT_DATETIME);
}
/**
* Return date d/m/Y
* @return string date
*/
public function getDateBR() {
return $this->date->format(self::$FORMAT_BR_DATE);
}
/**
* Return date d/m/Y H:i:s
* @return string date
*/
public function getDateTimeBR() {
return $this->date->format(self::$FORMAT_BR_DATETIME);
}
/**
* Return date Y-m-d
* @return string date
*/
public function getDateMySQLDate() {
return $this->date->format(self::$FORMAT_MYSQL_DATE);
}
/**
* Return date Y-m-d H:i:s
* @return string date
*/
public function getDateMySQLDateTime() {
return $this->date->format(self::$FORMAT_MYSQL_DATETIME);
}
}