Function that transforms the date into Brazilian and American format?

Asked

Viewed 397 times

0

How would a function that when called, turns 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 into 31/12/2017;

  • I had written wrong :(

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

Testing at Ideone.

Obs.: strtotime() does not work properly with /, so you need to make a replace of / for - in function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.