开发者

Converting date to this format

开发者 https://www.devze.com 2022-12-21 08:27 出处:网络
I have a date in this format: 24-12-2010 // DAY - MONTH - YEAR I need to get it in this format: 199开发者_Python百科5-12-31T23:59:59.999Z // The Z is for the TimeZone I think.

I have a date in this format:

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

I need to get it in this format:

   199开发者_Python百科5-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


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:

$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


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

精彩评论

暂无评论...
验证码 换一张
取 消