views:

95

answers:

4

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

A: 

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.
Snake
Curious, why the downvote on this one?
D_N
+3  A: 

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

gmdate('Y-m-d\TH:i:s\Z', strtotime($date_value));
Richard Harrison
I wouldn't suggest this way because it is error prone. Better provide the input format yourself, and use DateTime::W3C for the output format.
Snake
If the input format is fixed and predictable it is reliable (e.g. from a DB). If the input format is coming from the user then it is always necessary to reparse it.
Richard Harrison
A: 

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

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