Skip to content

Commit 9c72d9f

Browse files
update
1 parent fc8483f commit 9c72d9f

File tree

4 files changed

+114
-72
lines changed

4 files changed

+114
-72
lines changed

README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ Support Package For PHP and Laravel
218218
* [today](#today)
219219
* [yesterday](#yesterday)
220220
* [createFromFormat](#createFromFormat)
221+
* [createFromDateString](#createFromDateString)
221222
* [timestamp](#timestamp)
222223
* [toJsTimer](#toJsTimer)
223224
* [diff](#diff)
@@ -894,12 +895,20 @@ $time->yesterday();
894895
```
895896

896897
### createFromFormat
897-
- Accepts two parameter [date, format]
898-
- only [date] is mandatory and returns the Time(object)
898+
- Accepts two parameter [format, date]
899+
- only [format] is mandatory and returns the `string stamped formatted date`
899900

900901
```php
901-
$time->createFromFormat('24 Jan 2025 14:00:00', 'm/d/Y h:ia');
902-
// object(Tamedevelopers\Support\Time)
902+
$time->createFromFormat('m/d/Y h:ia', '24 Jan 2025 14:00:00');
903+
// 01/25/2025 02:00am
904+
```
905+
906+
### createFromDateString
907+
- Accepts one parameter [date]
908+
909+
```php
910+
$time->createFromFormat('24 Jan 2025 14:00:00');
911+
// 2025-01-24 14:00:00.000000
903912
```
904913

905914
### timestamp
@@ -949,10 +958,11 @@ $time->diffBetween('last year december', 1737752400, 'weeks');
949958
| `full \| short \| duration \| time \| date \| date_time \| time_stamp` |
950959

951960
```php
952-
$time->date('today')->ago()
953-
$time->date('today')->timeAgo()
961+
$time->date('today')->timeAgo('full')
962+
// 17 hours ago
954963

955964
// Output:
965+
$time->date('today')->ago()
956966
[
957967
"full" => "4 hours ago"
958968
"short" => "4h"

src/Time.php

Lines changed: 87 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -254,25 +254,6 @@ public function subYears($value = 0)
254254
{
255255
return $this->buildTimeModifier('year', $value, true);
256256
}
257-
258-
/**
259-
* Create timestamp
260-
*
261-
* @param int|string $date
262-
* - string|int|float
263-
*
264-
* @param string $format
265-
* - Your defined format type i.e: Y-m-d H:i:s a
266-
* - Converted TimeStamp
267-
*
268-
* @return string
269-
*/
270-
public static function timestamp($date, $format = "Y-m-d H:i:s")
271-
{
272-
$date = TimeHelper::setPassedDate($date);
273-
274-
return date($format, $date);
275-
}
276257

277258
/**
278259
* Set custom time
@@ -286,20 +267,6 @@ public static function date($date)
286267
return $base->setDate($date);
287268
}
288269

289-
/**
290-
* Create date from Format
291-
*
292-
* @param int|string $datetime
293-
* @param string $format
294-
* @return $this
295-
*/
296-
public static function createFromFormat($datetime, $format = 'm/d/Y h:i:sa')
297-
{
298-
return self::date(
299-
self::timestamp($datetime, $format)
300-
);
301-
}
302-
303270
/**
304271
* Format a date range.
305272
*
@@ -360,6 +327,52 @@ public static function yesterday()
360327
return self::date('yesterday');
361328
}
362329

330+
/**
331+
* Create timestamp
332+
*
333+
* @param int|string $date
334+
* - string|int
335+
*
336+
* @param string $format
337+
* - Your defined format type i.e: Y-m-d H:i:s a
338+
* - Converted TimeStamp
339+
*
340+
* @return string
341+
*/
342+
public static function timestamp($date, $format = "Y-m-d H:i:s")
343+
{
344+
$date = TimeHelper::setPassedDate($date);
345+
346+
return date($format, $date);
347+
}
348+
349+
/**
350+
* Create date from Format
351+
*
352+
* @param string $format
353+
* @param int|string|null $datetime
354+
* @return string
355+
*/
356+
public static function createFromFormat($format = 'Y-m-d H:i:s.u', $datetime = null)
357+
{
358+
return self::date(
359+
self::timestamp($datetime ?: 'now')
360+
)->format($format);
361+
}
362+
363+
/**
364+
* Create date from date string
365+
*
366+
* @param int|string $datetime
367+
* @return string
368+
*/
369+
public static function createFromDateString($datetime)
370+
{
371+
return self::date(
372+
self::timestamp($datetime)
373+
)->format('Y-m-d H:i:s.u');
374+
}
375+
363376
/**
364377
* Format time input
365378
*
@@ -374,8 +387,7 @@ public static function yesterday()
374387
public function format($format = null, $date = null)
375388
{
376389
if (!empty($date)) {
377-
$clone = self::date($date);
378-
$this->date = $clone->date;
390+
$this->date = TimeHelper::setPassedDate($date);
379391
}
380392

381393
if(empty($format)){
@@ -395,6 +407,26 @@ public function toDateTimeString()
395407
return $this->format();
396408
}
397409

410+
/**
411+
* toDateString
412+
*
413+
* @return string
414+
*/
415+
public function toDateString()
416+
{
417+
return $this->format('Y-m-d');
418+
}
419+
420+
/**
421+
* toTimeString
422+
*
423+
* @return string
424+
*/
425+
public function toTimeString()
426+
{
427+
return $this->format('H:i:s');
428+
}
429+
398430
/**
399431
* Create Javascript timer
400432
*
@@ -445,70 +477,70 @@ public static function config(?array $options = [])
445477
* Get the stored date time
446478
* @return int
447479
*/
448-
public function __getDate()
480+
public function __date()
449481
{
450482
return (int) $this->date;
451483
}
452484

453485
/**
454486
* Get the number of seconds between the stored time and the current time.
455-
* @return mixed
487+
* @return int
456488
*/
457-
public function __getSecond()
489+
public function __second()
458490
{
459491
return $this->__timeDifference('sec');
460492
}
461493

462494
/**
463495
* Get the number of minutes between the stored time and the current time.
464-
* @return mixed
496+
* @return int
465497
*/
466-
public function __getMin()
498+
public function __min()
467499
{
468500
return $this->__timeDifference('mins');
469501
}
470502

471503
/**
472504
* Get the number of hours between the stored time and the current time.
473-
* @return mixed
505+
* @return int
474506
*/
475-
public function __getHour()
507+
public function __hour()
476508
{
477509
return $this->__timeDifference('hour');
478510
}
479511

480512
/**
481513
* Get the number of days between the stored time and the current time.
482-
* @return mixed
514+
* @return int
483515
*/
484-
public function __getDay()
516+
public function __day()
485517
{
486518
return $this->__timeDifference('days');
487519
}
488520

489521
/**
490522
* Get the number of weeks between the stored time and the current time.
491-
* @return mixed
523+
* @return int
492524
*/
493-
public function __getWeek()
525+
public function __week()
494526
{
495527
return $this->__timeDifference('weeks');
496528
}
497529

498530
/**
499531
* Get the number of months between the stored time and the current time.
500-
* @return mixed
532+
* @return int
501533
*/
502-
public function __getMonth()
534+
public function __month()
503535
{
504536
return $this->__timeDifference('month');
505537
}
506538

507539
/**
508540
* Get the number of years between the stored time and the current time.
509-
* @return mixed
541+
* @return int
510542
*/
511-
public function __getYear()
543+
public function __year()
512544
{
513545
return $this->__timeDifference('year');
514546
}
@@ -609,7 +641,7 @@ public function __timeAgo($mode = null)
609641
$days = $diff['days'];
610642
$weeks = $diff['weeks'];
611643
$years = $diff['year'];
612-
$date = $this->__getDate();
644+
$date = $this->__date();
613645
$text = self::getText();
614646

615647
if ($days === 0 && $hours === 0 && $minutes < 1) {
@@ -667,12 +699,6 @@ public function __timeAgo($mode = null)
667699
return $data[$mode] ?? $data;
668700
}
669701

670-
/**
671-
* Get the text representation options.
672-
* @param string|null $mode
673-
*
674-
* @return mixed
675-
*/
676702
/**
677703
* Retrieve text configuration entries.
678704
*
@@ -706,12 +732,15 @@ public function __debugInfo(): array
706732
$time = (int) $this->date;
707733

708734
return [
709-
'timestamp' => $time,
735+
'date' => $time,
736+
'timestamp' => $this->timestamp,
710737
'formatted' => date('Y-m-d H:i:s', $time),
711738
'timezone' => (string) ($this->timezoneName ?? $this->timezone),
712739
'utc_offset' => ($this->utcOffset ?? date('(P)', $time)),
713740
'greeting' => $this->__greeting($time),
714741
'time_ago_short' => $this->__timeAgo('short'),
742+
'time_ago' => $this->__timeAgo(),
743+
'time_diff' => $this->__timeDifference(),
715744
];
716745
}
717746
}

src/Traits/TimeTrait.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,14 +303,14 @@ private static function nonExistMethod($method = null, $args = null, $clone = nu
303303
$method = match ($name) {
304304
'greetings', 'greeting' => '__greeting',
305305
'tojs', 'jstimer' => 'toJsTimer',
306-
's', 'sec', 'secs', 'getseconds', 'getsec' => '__getSecond',
307-
'min', 'mins', 'getminute', 'getminutes', 'getmin', 'getmins' => '__getMin',
308-
'hr', 'hrs', 'hour', 'hours', 'gethr', 'gethours', 'gethour' => '__getHour',
309-
'getday', 'getdays', 'getd', 'day', 'days' => '__getDay',
310-
'getweek', 'getweeks', 'week', 'weeks', 'getw' => '__getWeek',
311-
'getmonths', 'getmonth', 'getm', 'month', 'months' => '__getMonth',
312-
'getyr', 'getyears', 'getyear', 'year', 'years', 'yr', 'yrs', 'y' => '__getYear',
313-
'time', 'gettimes', 'gettime', 'getdate' => '__getDate',
306+
's', 'sec', 'secs', 'getseconds', 'getsec' => '__second',
307+
'min', 'mins', 'getminute', 'getminutes', 'getmin', 'getmins' => '__min',
308+
'hr', 'hrs', 'hour', 'hours', 'gethr', 'gethours', 'gethour' => '__hour',
309+
'getday', 'getdays', 'getd', 'day', 'days' => '__day',
310+
'getweek', 'getweeks', 'week', 'weeks', 'getw' => '__week',
311+
'getmonths', 'getmonth', 'getm', 'month', 'months' => '__month',
312+
'getyr', 'getyears', 'getyear', 'year', 'years', 'yr', 'yrs', 'y' => '__year',
313+
'time', 'gettimes', 'gettime', 'getdate' => '__date',
314314
'setdate' => '__setDate',
315315
'gettimezone' => '__getTimezone',
316316
'settimezone' => '__setTimezone',

tests/time.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
],
4545

4646
[
47+
$time->date('today')->timeAgo('full'),
48+
$time->createFromFormat('m/d/Y h:ia', '24 Jan 2025 14:00:00'),
49+
$time->createFromDateString('24 Jan 2025 14:00:00'),
4750
$time->startOfYear()->format(),
4851
$time->startOfDay()->format(),
4952
$time->startOfWeek()->format(),

0 commit comments

Comments
 (0)