Created
November 26, 2014 15:58
-
-
Save mingomax/267342adacd544e95679 to your computer and use it in GitHub Desktop.
Tratamento de diversos formato de data em PHP
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 | |
function date_grab($str) | |
{ | |
// regex pattern will match any date formatted dd-mm-yyy or d-m-y with | |
// separators: periods, slahes, dashes | |
$p = '{.*?(\d\d?)[\\/\.\-]([\d]{2})[\\/\.\-]([\d]{4}).*}'; | |
$date = preg_replace($p, '$3-$2-$1', $str); | |
return new \DateTime($date, new DateTimeZone('America/Sao_Paulo')); | |
} | |
$arr = array( | |
"03/12/2011 (Sat)", | |
"3.12.2011 SAT", | |
"Date: 03/12/2011 ", /* <-- the extra trailing space is intentional */ | |
"date:03/12/2011", | |
"date: 03/12/2011", | |
"03/12/2011", | |
"10/10/11" | |
); | |
foreach ($arr as $str) { | |
$date = date_grab($str); | |
echo $date->format('Y-m-d') . "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment