Skip to content

Instantly share code, notes, and snippets.

@chandeeland
Last active August 29, 2015 13:57
Show Gist options
  • Save chandeeland/9817516 to your computer and use it in GitHub Desktop.
Save chandeeland/9817516 to your computer and use it in GitHub Desktop.
/**
* DateTimeWrapper
*
* This class exists because you cannot modify the microseconds of a native PHP 5.5
* DateTime Object without creating a whole new instance.
*
* Additionally, this class allows you to serialize Datetime Objects without losing
* the microseconds.
*
* @copyright 2014 Mshanken Communications
* @author David Chan <[email protected]>
* @license BSD-3
*/
class DateTimeWrapper
{
private $datetime = null;
private $cold = null;
public function __construct(DateTime $datetime)
{
$this->datetime = $datetime;
}
public function __call($method, $params)
{
return call_user_func_array(array($this->datetime, $method), $params);
}
public static function __callStatic($method, $params)
{
return forward_static_call_array(array($this->datetime, $method), $params);
}
public function __sleep()
{
$this->cold = $this->datetime->format('Y-m-d G:i:s.u');
// unset($this->datetime);
return array('cold');
}
public function __wakeup()
{
$this->__construct(DateTime::createFromFormat('Y-m-d G:i:s.u', $this->cold));
}
public function getDateTime()
{
return $this->datetime;
}
}
@garak
Copy link

garak commented Mar 18, 2015

Is there any reason for using G instead of H for hours?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment