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(17)
    2. int(14)
    3. Now is 2020-09-24 17:14:49 +0300
    4. Tomarrow is Friday the 25th of September 2020
    5. Yesterday was Wednesday the 23rd of September 2020
    6. First day of this month was Tuesday and the next is Friday
    7.  and the last is Wednesday
    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-09-25 17:14:49.934775"
    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: 36
    3.    Monday August 31st
    4.    Tuesday September 1st
    5.    Wednesday September 2nd
    6.    Thursday September 3rd
    7.    Friday September 4th
    8.    Saturday September 5th
    9.    Sunday September 6th
    10.  week: 37
    11.    Monday September 7th
    12.    Tuesday September 8th
    13.    Wednesday September 9th
    14.    Thursday September 10th
    15.    Friday September 11th
    16.    Saturday September 12th
    17.    Sunday September 13th
    18.  week: 38
    19.    Monday September 14th
    20.    Tuesday September 15th
    21.    Wednesday September 16th
    22.    Thursday September 17th
    23.    Friday September 18th
    24.    Saturday September 19th
    25.    Sunday September 20th
    26.  week: 39
    27.    Monday September 21st
    28.    Tuesday September 22nd
    29.    Wednesday September 23rd
    30.    Thursday September 24th
    31.    Friday September 25th
    32.    Saturday September 26th
    33.    Sunday September 27th
    34.  week: 40
    35.    Monday September 28th
    36.    Tuesday September 29th
    37.    Wednesday September 30th
    38.    Thursday October 1st
    39.    Friday October 2nd
    40.    Saturday October 3rd
    41.    Sunday October 4th
    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