Last active
August 29, 2015 14:06
-
-
Save edwinheij/d208ce97e1dcf6f9765c to your computer and use it in GitHub Desktop.
datum en tijd omzetten naar welk formaat dan ook
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 | |
if ( ! function_exists('refdate')) | |
{ | |
/** | |
* $datum datum in wat dan ook voor formaat (ook unix) | |
* $outputformat d-m-Y / y-m-d / ... of unix / mysql | |
* result datum in gewenste formaat | |
* anders 'Error' en error message in log-file | |
* | |
* Gebruik met uitkomsten | |
* 01-02-2013 > 01-02-2013 | |
* 1-02-2013 > 01-02-2013 | |
* 1-02-13 > 01-02-2013 | |
* 31-13-2013 > Error (regex al fout, bestaat niet) | |
* 22-11-2013, 'unix' > 1385130986 | |
* 22112013 > 22-11-2013 | |
* 221113 > Error (te kort en/of zonder delimiters) | |
* 22/11/13 > 22-11-2013 | |
* 2013-2-1 > 01-02-2013 | |
* 30-2-13 > Error (datum bestaat niet) | |
* 1385127824, 'j-n-\'y' > 22-11-'13 | |
* 30-3-13, 'mysql' > 2013-03-30 | |
* */ | |
function refdate($datum, $outputformat = 'd-m-Y') // Recheck en Format Date | |
{ | |
try | |
{ | |
//echo $datum . ' - '; | |
switch ($outputformat) { | |
case "mysql" : | |
$outputformat = 'Y-m-d'; | |
break; | |
default : | |
} | |
if (preg_match('/^[\d]{10}$/', $datum) && isValidTimeStamp($datum)) { // unixTimeStamp | |
$datum = date('d-m-Y', $datum); | |
} | |
$regex_dag = '((?<j>[1-9]|1[0-9]|2[0-9]|3[0-1])|(?<d>0[1-9]|1[0-9]|2[0-9]|3[0-1]))'; | |
$regex_maand = '((?<n>[1-9]|1[0-2])|(?<m>0[1-9]|1[0-2]))'; | |
$regex_jaar = '((?<y>[\d]{2}|[\d]{2})|(?<Y>1[\d]{3}|2[\d]{3}))'; | |
$regexArr = array(); | |
$regexArr[] = $regex1 = '/^'.$regex_dag.'(?<delim>-?\/?)'.$regex_maand.'-?\/?'.$regex_jaar.'$/'; | |
$regexArr[] = $regex2 = '/^'.$regex_jaar.'(?<delim>-?\/?)'.$regex_maand.'-?\/?'.$regex_dag.'$/'; | |
foreach ($regexArr as $regex) { | |
if (preg_match($regex, $datum, $m)) { | |
break; | |
} | |
} | |
if ( !isset($m) || empty($m) ) { | |
throw new Exception('Datum niet juist (regex niet kunnen matchen)'); | |
} | |
if (strlen($datum) < 8 && empty ($m['delim'])) | |
throw new Exception('Datum niet betrouwbaar om te zetten (te kort en/of zonder delimiter)'); | |
$foundFormat = ''; | |
$wanted = 'd,m,Y,j,n,y'; | |
$haystack = explode(',', $wanted); | |
foreach ($m as $k => $v) | |
{ | |
if (in_array($k, $haystack) && $k != '0' && ! empty($v)) // && $k != 0) // && ! is_numeric($k)) | |
{ | |
$foundFormat .= $k.$m['delim']; | |
} | |
} | |
$foundFormat = ( ! empty ($m['delim'])) ? substr($foundFormat, 0, -1) : $foundFormat; | |
// match is made | |
if ( isset($m[0]) ) | |
{ | |
if (checkdate( | |
(! empty($m['m']) ? $m['m'] : str_pad($m['n'], 2, 0, STR_PAD_LEFT)), | |
(! empty($m['d']) ? $m['d'] : str_pad($m['j'], 2, 0, STR_PAD_LEFT)), | |
(! empty($m['Y']) ? $m['Y'] : $m['y']) | |
) | |
) | |
{ | |
$ret = DateTime::createFromFormat($foundFormat, $m[0]); | |
if ($outputformat == "unix") | |
{ | |
if ( $unixTimestamp = $ret->getTimestamp() ) // met tijd (uur/min/sec van huidigetijd) | |
{ | |
return $unixTimestamp; | |
} | |
else | |
{ | |
throw new Exception('Unix Timestamp moet tussen ' . date('d-m-Y', ~PHP_INT_MAX) . ' en ' . date('d-m-Y', PHP_INT_MAX) . ' liggen'); | |
} | |
} | |
else | |
{ | |
return $ret->format($outputformat); | |
} | |
} | |
else | |
{ | |
throw new Exception('Datum niet juist (checkdate result false)'); // zal weinig voorkomen aangezien regex eerst mis gaat | |
} | |
} | |
throw new Exception('Datum niet juist (iets onbekends fout gegaan)'); // zal weinig voorkomen | |
} | |
catch (Exception $e) | |
{ | |
if (function_exists('log_message')) log_message('error', '[' . __FUNCTION__ . '] ' . $e->getMessage() . ' (' . $datum .')'); | |
//echo "Error"; | |
return lang('error_refdate'); // let op: wordt ook op gechecked in functie 'refdate_zoeken' | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment