Skip to content

Instantly share code, notes, and snippets.

@macghriogair
Last active March 3, 2016 17:14
Show Gist options
  • Save macghriogair/5321aa26899a866a78f1 to your computer and use it in GitHub Desktop.
Save macghriogair/5321aa26899a866a78f1 to your computer and use it in GitHub Desktop.
custom datetime class
<?php
namespace Acme;
class DateTimeTest extends \PHPUnit_Framework_TestCase
{
/** @test */
public function it_creates_from_unix_timestamp()
{
$unix = time();
$acmeDate = new AcmeDateTime($unix);
$standardDate = new \DateTime("@".$unix);
$this->assertEquals($acmeDate, $standardDate);
}
/** @test */
public function it_creates_from_datestring()
{
$dateString = '2016/03/03';
$acmeDate = new AcmeDateTime($dateString);
$standardDate = new \DateTime($dateString);
$this->assertEquals($acmeDate, $standardDate);
}
}
class AcmeDateTime extends \DateTime
{
public function __construct($time = "now", DateTimeZone $timezone = null)
{
$time = is_numeric($time) ? "@" . $time : $time;
parent::__construct($time, $timezone);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment