Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting date to this format

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

like image 966
pesar Avatar asked Mar 01 '10 09:03

pesar


2 Answers

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

gmdate('Y-m-d\TH:i:s\Z', strtotime($date_value));
like image 58
Richard Harrison Avatar answered Sep 28 '22 09:09

Richard Harrison


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.

  • http://php.net/manual/en/class.datetime.php
  • http://php.net/manual/en/ref.datetime.php
like image 44
Christian Avatar answered Sep 28 '22 10:09

Christian