Function that turns the date into Brazilian and American format? [duplicate]

0

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 ;

    
asked by anonymous 22.11.2017 / 17:26

1 answer

0

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');
?>

Try Ideone .

Note: strtotime() does not work correctly with / , so you need to replace / to - in the function.

    
22.11.2017 / 18:17