Date format to save to the Bank

0

How to make date formatting in php, receive default date (d-m-Y) and save in the bank (Y-m-d).

This is the line of code used within input (Y-m_d)

value="<?php $date = new DateTime(''); $date->add(new DateInterval('P0D')); echo $date->format('Y-m-d'); ?>
">
    
asked by anonymous 22.06.2018 / 17:14

1 answer

1

There's more than one way to do what you're looking for, if you'd like to keep the value of input with snippets of code, you could do the following:

In your input, create a name property: <input name="data">

In the script where you treat the data coming from the form you can get the data via $_POST['data'] or $_GET['data'] depending on the method chosen to send the form.

At this time, I believe the user entered the date in the d-m-Y format, right? Ok ... You will do something like this to convert to Y-m-d:

$data = new DateTime($_POST['data']);

$dataFormatada = $data->format('Y-m-d') ;

Remembering that from now on you can usually work by putting intervals or whatever you need most and passing the responsibility of formatting the date to the script that handles the form after submission, you clean up your view a bit more and help readability and maintenance later.

    
22.06.2018 / 20:30