Created
November 27, 2012 12:08
-
-
Save djekl/4153916 to your computer and use it in GitHub Desktop.
This function tries to cope with as many numerical time and date formats as possible
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
public function epoch($_value = "", $_format = "Y-m-d H:i:s") | |
{ | |
// Description: This function tries to cope with as many numerical time and date | |
// formats as possible. | |
// Parameters: | |
// $_value: The time and/or date string (dd-mm-yyy, yyy-mm-dd, hh:mm:ss etc). | |
// $_format: The output format options (same as PHP "date" function). | |
$epoch = 0; | |
$pm = ''; | |
if(date("U", $_value) != $_value) { | |
if(strpos($_value, 'pm')) | |
$pm = 'pm'; | |
if(strpos($_value, 'am')) | |
$pm = 'am'; | |
$_value = str_replace('am', '', $_value); | |
$_value = str_replace('pm', '', $_value); | |
while(gettype(strpos($_value, ' ')) == 'integer') | |
$_value = str_replace(' ', ' ', $_value); | |
$_value = str_replace('-', '/', $_value); | |
$_value = str_replace('.', '/', $_value); | |
$_value = str_replace('\\', '/', $_value); | |
$datetime = explode(' ', $_value); | |
if(gettype(strpos($datetime[0], '/')) == 'integer') { | |
$date = $datetime[0]; | |
$time = count($datetime) == 2?$datetime[1]:''; | |
} | |
else { | |
$date = count($datetime) == 2?$datetime[1]:''; | |
$time = $datetime[0]; | |
} | |
$date = explode('/', $date); | |
if(count($date) != 3) | |
$date = explode('/', '00/00/0000'); | |
if(strlen($date[2]) > strlen($date[0])) | |
$date = array_reverse($date); | |
$time = explode(':', $time); | |
if($time[0] > 12) | |
$pm = ''; | |
if(($time[0] != 12) && ($pm == 'pm')) | |
$time[0] += 12; | |
else | |
if(($time[0] == 12) && ($pm == 'am')) | |
$time[0] += 12; | |
if($time[0] == 24) | |
$time[0] = 00; | |
$epoch = mktime(0, 0, 0, $date[1], $date[2], $date[0]); | |
if($epoch < 0) | |
$epoch = 0; | |
if(count($time) >= 1) | |
$epoch += (60 * 60 * $time[0]); | |
if(count($time) >= 2) | |
$epoch += (60 * $time[1]); | |
if(count($time) == 3) | |
$epoch += $time[2]; | |
} | |
else | |
$epoch = $_value; | |
if($_format) | |
return(date($_format, $epoch)); | |
else | |
return($epoch); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment