Skip to content

Instantly share code, notes, and snippets.

@michel-pi
Created October 12, 2020 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michel-pi/f0e98fae7a80b45033a7f0830177b870 to your computer and use it in GitHub Desktop.
Save michel-pi/f0e98fae7a80b45033a7f0830177b870 to your computer and use it in GitHub Desktop.
Formats DateTime for usage in JSON and MySQL Databases
<?php
namespace MichelPi\Utils;
use DateTime;
class DateTimeFormat
{
private static $_jsonFormatString = 'Y-m-d\\TH:i:s.v\\Z';
private static $_mysqlFormatString = 'Y-m-d H:i:s';
public static function toJsonString(DateTime $datetime): string
{
return $datetime->format(self::$_jsonFormatString);
}
public static function toMysqlString(DateTime $datetime): string
{
return $datetime->format(self::$_mysqlFormatString);
}
public static function fromJsonString(string $time): DateTime
{
return DateTime::createFromFormat(self::$_jsonFormatString, $time);
}
public static function fromMysqlString(string $time): DateTime
{
return DateTime::createFromFormat(self::$_mysqlFormatString, $time);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment