Last active
May 17, 2017 09:44
-
-
Save phudinsky/eab4fa8323ff68fbbdc42a1ae1f770d1 to your computer and use it in GitHub Desktop.
Very simple format converter from .net datetime format to php datetime format
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 | |
declare(strict_types=1); | |
class DotNetDateTimeFormatConverter | |
{ | |
/** | |
* @param string $dotNetDateTimeFormat | |
* @return string | |
*/ | |
public function toPhpDateTimeFormat(string $dotNetDateTimeFormat): string | |
{ | |
$mapping = $this->dotNetDateTimeFormatToPhpDateTimeFormatMapping(); | |
return strtr($dotNetDateTimeFormat, $mapping); | |
} | |
/** | |
* @return array | |
*/ | |
protected function dotNetDateTimeFormatToPhpDateTimeFormatMapping(): array | |
{ | |
return [ | |
'dddd' => 'l', | |
'ddd' => 'D', | |
'dd' => 'd', | |
'd' => 'j', | |
'fffffff' => null, | |
'ffffff' => null, | |
'fffff' => null, | |
'ffff' => null, | |
'fff' => null, | |
'ff' => null, | |
'f' => null, | |
'FFFFFFF' => null, | |
'FFFFFF' => null, | |
'FFFFF' => null, | |
'FFFF' => null, | |
'FFF' => null, | |
'FF' => null, | |
'F' => null, | |
'gg' => null, | |
'g' => null, | |
'HH' => 'H', | |
'H' => 'G', | |
'hh' => 'h', | |
'h' => 'g', | |
'K' => null, | |
'mm' => 'i', | |
'm' => null, | |
'MMMM' => 'F', | |
'MMM' => 'M', | |
'MM' => 'm', | |
'M' => 'n', | |
'ss' => 's', | |
's' => null, | |
'tt' => 'A', | |
't' => null, | |
'yyyyy' => null, | |
'yyyy' => 'Y', | |
'yyy' => null, | |
'yy' => 'y', | |
'y' => null, | |
'zzz' => 'P', | |
'zz' => null, | |
'z' => null, | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment