Generate a Solr tDate String in PHP

You can generate a Solr formatted tDate string using the Date() function in PHP. In order to do so, you'll need to escape the “T” and the “Z” in the date string. Here's an example to get a Solr tDate of one year ago.

$tDate = date('Y-m-d\TH\\\\:i\\\\:s\Z', strtotime("-1 years"));

If you want the beginning of the day one year ago (instead of exactly 1 year ago to the second) you can use 00:00:00 as your time. Here's an example with that.

$tDate = date('Y-m-d\T00\\\\:00\\\\:00\Z', strtotime("-1 years"));

The quadruple backslashes create single backslashes in the $tDate result. Those backslashes are then required by Solr to escape the colons. So, $tDate will contain something like the following.

2011-09-07T15\:41\:48Z
comments powered by Disqus
php/solr_tdate.txt · Last modified: 2020/06/01 22:53 (external edit)