Receive date and time without separators and convert to date format

Asked

Viewed 342 times

2

I would like to have the date and time in php, that way:

Example:

I’d like to receive that date:

09092017104430

And I wish the way out was something like this:

09/09/2017 10:44:30

1 answer

3

Use the method DateTime::createFromFormat. You must pass the date input format in the first parameter. In the second parameter, you must put the string containing the value that will be interpreted as a date.

Behold:

$data = DateTime::createFromFormat('dmYHis', '09092017104430');


var_dump($data->format('d/m/Y H:i:s'));

The style above was written using OOP. But if you prefer, you can also use the functions below to do this:

$data = date_create_from_format('dmYHis', '09092017104430');


var_dump(date_format($data, 'd/m/Y H:i:s'));

Explanation of each value used in the format:

d => double-digit day

m => double-digit month

Y => Year with 4 digits

H => Current time, from 0 to 23, with two digits

i => two-digit minute

s => second with two digits

Browser other questions tagged

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