Convert 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 out this link:

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

The above link is how I need the date.

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

thanks

+4
source share
5 answers

This is an ISO8601 format date; What would you like.

 gmdate('Ymd\TH:i:s\Z', strtotime($date_value)); 
+9
source

You can do something like this:

 $dateTime = new DateTime($myDate); $formatted = $dateTime->format("Ymd\TH:i:sz\Z"); 

Specified solution with:

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

returns strings like:

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

which cannot be analyzed, at least with newer versions of Solr.

I would not use gmdate if you need to support time zones. The implementation of DateTime is well executed and also available for functional programming.

+5
source

You can use datetime class

 $dateTime = new DateTime(); $dateTime.setDate(24, 12, 2010); $output = $dateTime.format(DateTime::W3C); // Output now is your date in W3C format. 
+2
source

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

0
source
 $date = strtotime('24-12-2010'); $new_date = gmDate("Ymd\TH:i:sz\Z",$date); 
0
source

Source: https://habr.com/ru/post/1302718/


All Articles