How would a function that when called make any date (valid) to the contrary?
For example, I typed:
21/04/2017
will return as 2017/04/21
;
If I type:
2017/12/31
will transform to 31/12/2017
;
How would a function that when called make any date (valid) to the contrary?
For example, I typed:
21/04/2017
will return as 2017/04/21
;
If I type:
2017/12/31
will transform to 31/12/2017
;
This function does the conversion using strtotime()
:
<?php
function convData($i){
$i = str_replace('/','-',$i);
return str_replace('-','/',strlen(end(explode('-',$i))) == 4 ?
date("Y-m-d", strtotime($i)) : date("d-m-Y", strtotime($i)));
}
echo convData('21/04/2017');
echo '-----';
echo convData('2017/04/21');
?>
Note: strtotime()
does not work correctly with /
, so you need to replace
/
to -
in the function.