Home » Php » php – Converting date to this format

php – Converting date to this format

Posted by: admin July 12, 2020 Leave a comment


I have a date in this format:

   24-12-2010 // DAY - MONTH - YEAR

I need to get it in this format:

   1995-12-31T23:59:59.999Z // The Z is for the TimeZone I think.

Check this link out:


The above link is the way I need the date.

I am using PHP now, so this needs to be with PHP.
How can I convert these dates the easiest way?


How to&Answers:

That is an ISO8601 format date; the following is what you want.

gmdate('Y-m-d\TH:i:s\Z', strtotime($date_value));


You can do something like that:

$dateTime = new DateTime($myDate);
$formatted = $dateTime->format("Y-m-d\TH:i:s.z\Z");

The mentioned solution with:


does return strings like:


which cannot be parsed, at least with newer Solr versions.

I wouldn’t use gmdate if you need to support timezones. The DateTime implementation is well done, and is also available for functional programming.


You can use the DateTime class

$dateTime = new DateTime();
$dateTime.setDate(24, 12, 2010);

$output = $dateTime.format(DateTime::W3C);

// Output now is your date in W3C format.


use the date ( string $format [, int $timestamp ] ) function of php!
In second paramter use http://php.net/manual/en/function.strtotime.php to get the timestamp from strings


$date = strtotime('24-12-2010');
$new_date = gmDate("Y-m-d\TH:i:s.z\Z",$date);