How to convert a date to this format in PHP?

Asked

Viewed 276 times

6

Can anyone tell me what kind of date this format is?

2013-09-17T05:59:00+01:00

I have in the database a timestamp or date field. How to convert its value to this format using PHP?

  • This date needs to appear like this in php?

  • 3

    the format of the date and ISO 8601.

  • 2

    I think it was not clear the edition made. I had to open the history to understand the question. Anyway, see if this question helps you with something.

  • 1

    This question is being debated here. And @Kaduamaral, this is very pertinent to you.

  • Thanks @Victorstafusa, I saw the debate there, and commented there.

1 answer

8


You can get a date in this format, from a record of your database MySQL, in this way:

echo date("Y-m-d\TH:m:sP", strtotime($db_date));

Where the variable $db_date is the field you will get from your database (regardless of type date or timestamp) and the exit is exactly the one you put in your question.

In the documentation of PHP for the function date() you find all these formats that I used in the first parameter. In the second parameter, I just converted your date to timestamp Unix through function strtotime().


As well as the rray quoted here, a more simplified form is using the parameter c, which already returns a date in the format ISO 8601:

echo date("c", strtotime($db_date));

Browser other questions tagged

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