Convert normal numbers to date

Asked

Viewed 31 times

3

I wonder, if there is how to convert normal number to date format in PHP.

Example, a user types 09012001 into a input of type text and the value returned in a echo further down be 09/01/2001

This is possible in PHP? if yes, what would be the most practical way to achieve?

1 answer

3


Yeah, basically what a date, utilize date_create_from_format to create an object of the type Datetime if the data is valid if it does not return bool(false) for invalid data, example:

$data = date_create_from_format('dmY', '09012001');
if ($data) // se for data válida
{ 
    echo $data->format('d/m/Y');
}

  • 1

    It worked out, bro!

Browser other questions tagged

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