Skip to content

Commit

Permalink
Add a DateTime::strtotime() method
Browse files Browse the repository at this point in the history
  • Loading branch information
duncan3dc committed Aug 14, 2017
1 parent 55de616 commit 942a430
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,14 @@ Changelog

--------

## 1.3.0 - 2017-08-14

### Added

* [DateTime] Add a strtotime() method to make use of the standard php parsing.

--------

## 1.2.1 - 2017-07-24

### Added
Expand Down
15 changes: 15 additions & 0 deletions src/DateTime.php
Expand Up @@ -93,6 +93,21 @@ public static function mktime($hour, $minute, $second, $month, $day, $year)
}


/**
* Convert a free format date to an instance.
*
* @param string $date The date to parse
*
* @return static
*/
public static function strtotime($date)
{
$unix = strtotime($date);

return new static($unix);
}


/**
* Create a new instance from a unix timestamp.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/DateTimeTest.php
Expand Up @@ -3,6 +3,7 @@
namespace duncan3dc\DateTests;

use duncan3dc\Dates\DateTime;
use duncan3dc\Dates\Interfaces\Days;

class DateTimeTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -33,6 +34,19 @@ public function testNow()
}


public function testStrtotime1()
{
$expected = DateTime::now()->getNext(Days::TUESDAY);
$date = DateTime::strtotime("next tuesday");
$this->assertSame($expected->format("Y-m-d"), $date->format("Y-m-d"));
}
public function testStrtotime2()
{
$this->setExpectedException(\InvalidArgumentException::class, "An invalid unix timestamp was passed");
DateTime::strtotime("nope");
}


public function testFormat1()
{
$unix = time();
Expand Down

0 comments on commit 942a430

Please sign in to comment.