Created
October 12, 2020 22:34
-
-
Save michel-pi/f0e98fae7a80b45033a7f0830177b870 to your computer and use it in GitHub Desktop.
Formats DateTime for usage in JSON and MySQL Databases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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