Skip to content

Instantly share code, notes, and snippets.

@phudinsky
Last active May 17, 2017 09:44
Show Gist options
  • Save phudinsky/eab4fa8323ff68fbbdc42a1ae1f770d1 to your computer and use it in GitHub Desktop.
Save phudinsky/eab4fa8323ff68fbbdc42a1ae1f770d1 to your computer and use it in GitHub Desktop.
Very simple format converter from .net datetime format to php datetime format
<?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