Skip to content

Commit f037d1e

Browse files
committed
accept short format (just minutes and seconds)
1 parent 1e6f6ee commit f037d1e

File tree

4 files changed

+94
-60
lines changed

4 files changed

+94
-60
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ To create an object you have the next options.
1515
```php
1616
$time = EasyTime::createFromSeconds(20465);
1717
$time = EasyTime::createFromFormat('10:30:00');
18+
$time = EasyTime::createFromFormat('32:10');
1819
$time = EasyTime::createFromFormat('2:10:30:00'); // With days
1920
$time = EasyTime::create(0, 10, 30, 00); // Days, Hours, Minutes, Seconds
2021
```
@@ -33,6 +34,7 @@ $time->day; // 2
3334
$time->getDays(); // 2.42 (Total days in all that time)
3435
$time->format(); // '58:30:00'
3536
$time->format('full'); // '2:10:30:00'
37+
$time->format('short'); // '30:00'
3638
```
3739

3840
## Sum of two objects

src/EasyTime.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,45 @@
22

33
namespace WeblaborMX\EasyTime;
44

5-
class EasyTime {
6-
5+
class EasyTime
6+
{
77
public static $time;
88

9-
public static function create($days, $hours, $minutes, $seconds) {
9+
public static function create($days, $hours, $minutes, $seconds)
10+
{
1011
self::$time = new Time($days, $hours, $minutes, $seconds);
1112
return self::$time;
1213
}
1314

14-
public static function createFromFormat($data) {
15+
public static function createFromFormat($data)
16+
{
1517
$data = explode(':', $data);
16-
if(count($data)==3)
18+
if(count($data)==3) {
1719
self::$time = new Time(0, $data[0], $data[1], $data[2]);
18-
if(count($data)>3)
20+
}
21+
if(count($data)==2) {
22+
self::$time = new Time(0, 0, $data[0], $data[1]);
23+
}
24+
if(count($data)>3) {
1925
self::$time = new Time($data[0], $data[1], $data[2], $data[3]);
26+
}
2027
return self::$time;
2128
}
2229

23-
public static function createFromSeconds($seconds) {
30+
public static function createFromSeconds($seconds)
31+
{
2432
$result = self::secondsToTime($seconds);
2533
self::$time = new Time($result['d'], $result['h'], $result['m'], $result['s']);
2634
return self::$time;
2735
}
2836

29-
public static function sum($object, $object2) {
37+
public static function sum($object, $object2)
38+
{
3039
return $object->addTime($object2);
3140
}
3241

33-
private static function secondsToTime($inputSeconds) {
42+
private static function secondsToTime($inputSeconds)
43+
{
3444

3545
$secondsInAMinute = 60;
3646
$secondsInAnHour = 60 * $secondsInAMinute;
@@ -60,6 +70,4 @@ private static function secondsToTime($inputSeconds) {
6070
);
6171
return $obj;
6272
}
63-
64-
}
65-
73+
}

src/Time.php

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?php
2-
32
namespace WeblaborMX\EasyTime;
43

5-
class Time {
6-
4+
class Time
5+
{
76
public $day;
87
public $hour;
98
public $minute;
@@ -27,7 +26,8 @@ class Time {
2726
]
2827
];
2928

30-
public function __construct($day, $hour, $minute, $second) {
29+
public function __construct($day, $hour, $minute, $second)
30+
{
3131
if($second >= 60 || $minute >= 60 || $hour >= 24)
3232
throw new \Exception("Error Format", 1);
3333

@@ -37,9 +37,13 @@ public function __construct($day, $hour, $minute, $second) {
3737
$this->second = (int) $second;
3838
}
3939

40-
public function format($format = 'normal') {
41-
if($format=='full')
40+
public function format($format = 'normal')
41+
{
42+
if($format=='full') {
4243
return $this->day.':'.sprintf('%02d', $this->hour).':'.sprintf('%02d', $this->minute).':'.sprintf('%02d', $this->second);
44+
} else if($format=='short') {
45+
return sprintf('%02d', $this->minute).':'.sprintf('%02d', $this->second);
46+
}
4347
$hour = $this->day * 24;
4448
$hour = $hour + $this->hour;
4549
return sprintf('%02d', $hour).':'.sprintf('%02d', $this->minute).':'.sprintf('%02d', $this->second);
@@ -49,30 +53,34 @@ public function format($format = 'normal') {
4953
* Convert to one kind of time
5054
*/
5155

52-
public function getSeconds() {
56+
public function getSeconds()
57+
{
5358
$seconds = $this->day*24*60*60;
5459
$seconds += $this->hour*60*60;
5560
$seconds += $this->minute*60;
5661
$seconds += $this->second;
5762
return $seconds;
5863
}
5964

60-
public function getMinutes() {
65+
public function getMinutes()
66+
{
6167
$minutes = $this->day*24*60;
6268
$minutes += $this->hour*60;
6369
$minutes += $this->minute;
6470
$minutes += round(((100 / 60) * $this->second) * 0.01, 2);
6571
return $minutes;
6672
}
6773

68-
public function getHours() {
74+
public function getHours()
75+
{
6976
$hours = $this->day*24;
7077
$hours += $this->hour;
7178
$hours += round(((100 / 60) * $this->minute) * 0.01, 2);
7279
return $hours;
7380
}
7481

75-
public function getDays() {
82+
public function getDays()
83+
{
7684
$hours = $this->day;
7785
$hours += round(((100 / 24) * $this->hour) * 0.01, 2);
7886
return $hours;
@@ -82,7 +90,8 @@ public function getDays() {
8290
* Humans
8391
*/
8492

85-
public function diffForHumans($lang = 'en') {
93+
public function diffForHumans($lang = 'en')
94+
{
8695
$result = '';
8796
if($this->day > 1) {
8897
$result .= $this->day.' '.$this->lang[$lang]['days'];
@@ -114,7 +123,8 @@ public function diffForHumans($lang = 'en') {
114123
* Sum of objects
115124
*/
116125

117-
public function addTime($time) {
126+
public function addTime($time)
127+
{
118128
$seconds = $this->getSeconds();
119129
$seconds += $time->getSeconds();
120130
return EasyTime::createFromSeconds($seconds);
@@ -124,106 +134,122 @@ public function addTime($time) {
124134
* Additions
125135
*/
126136

127-
public function addSeconds($seconds) {
137+
public function addSeconds($seconds)
138+
{
128139
$seconds += $this->getSeconds();
129140
return EasyTime::createFromSeconds($seconds);
130141
}
131142

132-
public function addSecond() {
143+
public function addSecond()
144+
{
133145
return $this->addSeconds(1);
134146
}
135147

136-
public function addMinutes($minutes) {
148+
public function addMinutes($minutes)
149+
{
137150
$seconds = $minutes*60;
138151
$seconds += $this->getSeconds();
139152
return EasyTime::createFromSeconds($seconds);
140153
}
141154

142-
public function addMinute() {
155+
public function addMinute()
156+
{
143157
return $this->addMinutes(1);
144158
}
145159

146-
public function addHours($hours) {
160+
public function addHours($hours)
161+
{
147162
$seconds = $hours*60*60;
148163
$seconds += $this->getSeconds();
149164
return EasyTime::createFromSeconds($seconds);
150165
}
151166

152-
public function addHour() {
167+
public function addHour()
168+
{
153169
return $this->addHours(1);
154170
}
155171

156-
public function addDays($day) {
172+
public function addDays($day)
173+
{
157174
$seconds = $day*24*60*60;
158175
$seconds += $this->getSeconds();
159176
return EasyTime::createFromSeconds($seconds);
160177
}
161178

162-
public function addDay() {
179+
public function addDay()
180+
{
163181
return $this->addDays(1);
164182
}
165183

166184
/**
167185
* Substractions
168186
*/
169187

170-
public function subSeconds($seconds) {
188+
public function subSeconds($seconds)
189+
{
171190
$result = $this->getSeconds();
172191
$result -= $seconds;
173192
return EasyTime::createFromSeconds($result);
174193
}
175194

176-
public function subSecond() {
195+
public function subSecond()
196+
{
177197
return $this->subSeconds(1);
178198
}
179199

180-
public function subMinutes($minutes) {
200+
public function subMinutes($minutes)
201+
{
181202
$seconds = $this->getSeconds();
182203
$seconds -= $minutes*60;
183204
return EasyTime::createFromSeconds($seconds);
184205
}
185206

186-
public function subMinute() {
207+
public function subMinute()
208+
{
187209
return $this->subMinutes(1);
188210
}
189211

190-
public function subHours($hours) {
212+
public function subHours($hours)
213+
{
191214
$seconds = $this->getSeconds();
192215
$seconds -= $hours*60*60;
193216
return EasyTime::createFromSeconds($seconds);
194217
}
195218

196-
public function subHour() {
219+
public function subHour()
220+
{
197221
return $this->subHours(1);
198222
}
199223

200-
public function subDays($day) {
224+
public function subDays($day)
225+
{
201226
$seconds = $this->getSeconds();
202227
$seconds -= $day*24*60*60;
203228
return EasyTime::createFromSeconds($seconds);
204229
}
205230

206-
public function subDay() {
231+
public function subDay()
232+
{
207233
return $this->subDays(1);
208234
}
209235

210236
/**
211237
* Operations
212238
*/
213239

214-
public function multiply($number) {
240+
public function multiply($number)
241+
{
215242
$seconds = $this->getSeconds();
216243
$result = $seconds * $number;
217244
$result = ceil($result);
218245
return EasyTime::createFromSeconds($result);
219246
}
220247

221-
public function divide($number) {
248+
public function divide($number)
249+
{
222250
$seconds = $this->getSeconds();
223251
$result = $seconds / $number;
224252
$result = ceil($result);
225253
return EasyTime::createFromSeconds($result);
226254
}
227-
228-
}
229-
255+
}

0 commit comments

Comments
 (0)