Date and Time

PHP has a variety of functions and classes that can handle date andd time related operations. This namespace introduces some addons to these native properties.

Interfaces for dates and times

The DateInterface interface
operations for dates
The DateTimeInterface interface
operations for both dates and times like for hours, minutes, seconds and timezones

The Date and The DateTime classes

These classes are wrappers for PHP's native DateTimeImmutable.

The Date class implements DateInterface. It ignores time units smaller than days (like hours, minutes and seconds).

The DateTime class introduces some new ways to compare datetimes.

  • DateTime example
    1. <?php
    2.  
    3. namespace Sphp\DateTime;
    4.  
    5. $now = new DateTime();
    6. var_dump($now->getHours(), $now->getMinutes());
    7. echo "Now is " . $now->format('Y-m-d H:i:s O') . "\n";
    8. echo "Tomarrow is " . $now->nextDay()->format('l \t\h\e jS \of F Y') . "\n";
    9. echo "Yesterday was " . $now->previousDay()->format('l \t\h\e jS \of F Y') . "\n";
    10. echo "First day of this month was " . $now->firstOfMonth()->format('l');
    11.  
    12. echo " and the next is " . $now->add('P1D')->format('l') . "\n";
    13.  
    14. echo " and the last is " . $now->sub('P1D')->format('l') . "\n";
    15.  
    16.  
    17. var_dump($now->add('P1D'));
    18.  
    Highlighted with GeSHi 1.0.9.1
  • DateTime example results
    1. int(13)
    2. int(47)
    3. Now is 2020-05-25 13:47:06 +0300
    4. Tomarrow is Tuesday the 26th of May 2020
    5. Yesterday was Sunday the 24th of May 2020
    6. First day of this month was Friday and the next is Tuesday
    7.  and the last is Sunday
    8. object(Sphp\DateTime\DateTime)#1543 (2) {
    9.   ["reflector":"Sphp\DateTime\DateTime":private]=>
    10.   NULL
    11.   ["dateTime":"Sphp\DateTime\AbstractDate":private]=>
    12.   object(DateTimeImmutable)#1607 (3) {
    13.     ["date"]=>
    14.     string(26) "2020-05-26 13:47:06.736094"
    15.     ["timezone_type"]=>
    16.     int(3)
    17.     ["timezone"]=>
    18.     string(15) "Europe/Helsinki"
    19.   }
    20. }
    21.  
    Highlighted with GeSHi 1.0.9.1

The Period class

This class extends PHP's native DatePeriod and thus represents a date period. A date period allows iteration over a set of dates and times, recurring at regular intervals, over a given period.

Note: Periods is a factory for Period objects.

  • Factoring Weeks of month code
    1. <?php
    2.  
    3. namespace Sphp\DateTime;
    4.  
    5. $p = Periods::weeksOfMonth();
    6. $start = $p->getStartDate();
    7.   echo $start->format('Y');
    8. foreach ($p as $date) {
    9.   echo "\n week: {$date->format('W')}";
    10.   foreach (Periods::days($date, 6) as $wd) {
    11.     echo "\n   {$wd->format('l F jS')} ";
    12.   }
    13. }
    14.  
    Highlighted with GeSHi 1.0.9.1
  • Periods factory results
    1. 2020
    2.  week: 18
    3.    Monday April 27th
    4.    Tuesday April 28th
    5.    Wednesday April 29th
    6.    Thursday April 30th
    7.    Friday May 1st
    8.    Saturday May 2nd
    9.    Sunday May 3rd
    10.  week: 19
    11.    Monday May 4th
    12.    Tuesday May 5th
    13.    Wednesday May 6th
    14.    Thursday May 7th
    15.    Friday May 8th
    16.    Saturday May 9th
    17.    Sunday May 10th
    18.  week: 20
    19.    Monday May 11th
    20.    Tuesday May 12th
    21.    Wednesday May 13th
    22.    Thursday May 14th
    23.    Friday May 15th
    24.    Saturday May 16th
    25.    Sunday May 17th
    26.  week: 21
    27.    Monday May 18th
    28.    Tuesday May 19th
    29.    Wednesday May 20th
    30.    Thursday May 21st
    31.    Friday May 22nd
    32.    Saturday May 23rd
    33.    Sunday May 24th
    34.  week: 22
    35.    Monday May 25th
    36.    Tuesday May 26th
    37.    Wednesday May 27th
    38.    Thursday May 28th
    39.    Friday May 29th
    40.    Saturday May 30th
    41.    Sunday May 31st
    42.  week: 23
    43.    Monday June 1st
    44.    Tuesday June 2nd
    45.    Wednesday June 3rd
    46.    Thursday June 4th
    47.    Friday June 5th
    48.    Saturday June 6th
    49.    Sunday June 7th
    Highlighted with GeSHi 1.0.9.1

The Interval class

The Interval class extends PHP's native DateInterval class and implements IntervalInterface.

An interval stores either a fixed amount of time (in years, months, days, hours etc) or a relative time string in the format that DateTime's constructor supports.

Note: Intervals is factory for Interval objects. Interval objects can be created from other interval objects, strings and integers.

  • Intervals factory example
    1. <?php
    2.  
    3. namespace Sphp\DateTime;
    4.  
    5. //var_dump(Intervals::create('+2 days')->toDays());
    6. echo Intervals::create('+2 days');
    7. var_dump((string)Intervals::create('PT2H')->toHours());
    8. var_dump(Intervals::create('PT2H')->toHours());
    9. var_dump(Intervals::create('0:01:21')->toSeconds());
    10. var_dump(Intervals::create(-62)->toMinutes());
    11.  
    12. $interval = Intervals::create(-112412e9);
    13. var_dump(Intervals::toString($interval));
    14.  
    Highlighted with GeSHi 1.0.9.1
  • Intervals factory results
    1. P2Dstring(1) "2"
    2. float(2)
    3. float(81)
    4. float(1.0333333333333)
    5. string(1) "P"
    6.  
    Highlighted with GeSHi 1.0.9.1