Convert the date format the user types

Asked

Viewed 233 times

2

Hello, I have the following code to format the date that the user informs and inserts in my database:

$arrayData['data_validade'] = date_format("Y-m-d",strtotime(str_replace('/','-',$this->$arrayData)));

Only, this date_format, works only for the current date, but I do not want the current date, I want the date that the user informs through my datapicker.

Any suggestions?

3 answers

1


That way I managed to solve:

$arrayData['data_validade'] = implode("-", array_reverse(explode("/", $arrayData['data_validade'])));

0

I believe that the best way to revolve situations of the type, is with the object Datetime. It is always good to try to use the new features of PHP.

$data = '17/07/2014';
$data_en = DateTime::createFromFormat('d/m/Y', $data);
print_r($data_en->format('Y-m-d')); //2014-07-17

0

If you are using postgres, you can format the date at any time even in an Internet, other than mysql.

Before the main query this line will convert all fields dates of YYYY-MM-DD for DD/MM/YYYY at once.

SET DATESTYLE TO SQL, DMY;
INSERT INTO tabela(data) VALUES('31/12/2015');

Browser other questions tagged

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