Date formatting to save to Database

Asked

Viewed 128 times

0

How to do Date Formatting in php, receive default date (d-m-Y) and save to Database (Y-m-d).

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

value="<?php $date = new DateTime(''); $date->add(new DateInterval('P0D')); echo $date->format('Y-m-d'); ?>
">

1 answer

1


There is more than one way to do what you want, if you want to keep the value of input with snippets of code, you could do the following:

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

In the script where you process the data coming from the form you can take the data via $_POST['data'] or $_GET['data'] depending on the method chosen for sending the form.

At this point, I believe the user entered the date in 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 then on you can work normally by putting intervals or whatever you need and passing the responsibility of formatting the date to the script that handles the form after sending, you clean a little more your view and helps in readability and maintenance later.

Browser other questions tagged

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