2

I have a date in the ymd format. Example for 2014-05-28 I actually have: 140528.

I would like to convert it to the date(c") format. Example: 2014-05-28T00:00:00-04:00

I cannot use DateTime::createFromFormat because of my php version. So instead I tried strtotime, but with no success:

$d[14]="140528";
$deliveryDate = date('ymd', $d[14]);
$d[14] = date("c", strtotime($deliveryDate));

strtotime is not recognizing the original date format as it returns me the 1969 date.

0

1 Answer 1

2

Break it up into parts and then form a valid date out of those parts. Then strtotime() will work.

$badDate = "140528";
$date = sprintf("20%s-%s-%s",
    substr($badDate, 0, 2),
    substr($badDate, 2, 2),
    substr($badDate, 4, 2)
);

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

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

Works well, thanks! Don't like the hard coded "20" bit, but I guess it will do for the next 85 years.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.