Home » Php » php – Converting date to this format

php – Converting date to this format

Posted by: admin July 12, 2020 Leave a comment

Questions:

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:

http://lucene.apache.org/solr/api/org/apache/solr/schema/DateField.html

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?

Thanks

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));

Answer:

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:

$dateTime->format(DateTime::W3C);
$dateTime->format(DateTime::ISO8601);

does return strings like:

2012-11-28T17:21:11+0100

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.

Answer:

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.

Answer:

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

Answer:

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