21

My input date is 2014-03-10 05:40:00. How can I convert it to RFC format like 2014-3-10T05:40:00.000-00:00?

3 Answers 3

32

The date function in PHP 5 provides another option:

$datetime = date("c", strtotime("2014-03-10 05:40:00"));
echo $datetime; // Output: 2014-03-10T05:40:00+00:00 
1
  • 1
    This doesn't include the time zone offset, which is crucial.
    – Flimm
    Commented Dec 2 at 14:11
28

RFC3339 is one of the predefined format constants of the DateTime class.

$inputDate = "2014-03-10 05:40:00";

$datetime = \DateTime::createFromFormat("Y-m-d H:i:s", $inputDate);

echo $datetime->format(\DateTime::RFC3339);
1
  • 1
    Sadly, this code doesn't have time zone offsets, which is crucial. The question does include a time zone offset.
    – Flimm
    Commented Dec 2 at 14:12
19

I'd like to add that the predefined constants for this can also be used with date(). All of these:

date(DATE_RFC3339); // '2024-12-02T14:12:43+00:00'
date(DATE_ATOM);    // '2024-12-02T14:12:43+00:00'
date(DATE_W3C);     // '2024-12-02T14:12:43+00:00'

return an RFC3339 formatted date time string and are the equivalent of:

date('Y-m-d\TH:i:sP'); // '2024-12-02T14:12:43+00:00'
1
  • 1
    Excellent! This answer does include the time zone offset.
    – Flimm
    Commented Dec 2 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.