I have an object that has a property that is a DateTime object that has a set timezone depending on the user's timezone. At some point I need to store the object where in my storage layer the property is a UTC datetime string, however I may need to continue using the object after it has been persisted and currently my approach converts the object to another timezone and then sets it back to what it used to be:
/* "starts" is the name of the property and this is just a "magic" method my driver
will call before persisting the object. */
public function __persist_starts() {
$tz = $this->starts->getTimezone();
$this->starts->setTimezone(new DateTimeZone("UTC"));
$value = $this->starts->format('Y-m-d H:i:s');
$this->starts->setTimezone($tz);
return $value;
}
This appears to be a "hacky" solution, isn't there something clearer I can use. I'm imagining something like
public function __persist_starts() {
return $this->starts->formatUTC('Y-m-d H:i:s');
}
formatUTC()code, in the same way Carbon extends DateTime and provides a series of built-in methods for different formats