另一个dateTime问题

pes*_*sar 2 php mysql datetime solr

我目前有这种格式的日期

2010-03-03 10:39:18
Run Code Online (Sandbox Code Playgroud)

这是TIMESTAMPMySQL中的一个字段.对于名为Solr的搜索引擎,我需要这种格式的日期:

1995-12-31T23:59:59Z
Run Code Online (Sandbox Code Playgroud)

以下是他们网站上关于日期的一些文字:

Solr期望索引时日期为UTC.此日期字段的格式为1995-12-31T23:59:59Z格式,是dateTime的规范表示形式的更受限制的形式 http://www.w3.org/TR/xmlschema-2/#dateTime.尾随"Z"表示UTC时间并且是强制性的.允许使用可选的小数秒:1995-12-31T23:59:59.999Z所有其他组件都是必需的.

我在这里得到了来自另一个Q的代码,但它没有用.Solr抱怨"无效的时间字符串":

$solr_date = date('c', (strtotime($date_from_mysql)); // doesn't work
Run Code Online (Sandbox Code Playgroud)

当回应上面提到$solr_date,的尾随Z不存在时.谢谢.

Phi*_*ord 5

你为什么不把它转换成UTC?

    $datetime = "2010-01-19 00:00:00";
    echo "Datetime Before: ".$datetime."<br />";
    $dttostr = strtotime("2010-01-19 00:00:00");
    echo "Datetime After: ".formatToUTC($dttostr)."<br />";
    echo "System Timezone: ".date_default_timezone_get()."<br />";

    function formatToUTC($passeddt) {
        // Get the default timezone
        $default_tz = date_default_timezone_get();

        // Set timezone to UTC
        date_default_timezone_set("UTC");

        // convert datetime into UTC
        $utc_format = date("Y-m-d\TG:i:s\Z", $passeddt);

        // Might not need to set back to the default but did just in case
        date_default_timezone_set($default_tz);

        return $utc_format;
    }
Run Code Online (Sandbox Code Playgroud)

  • 根据ISO 8601规范,您需要在所有字段上都有前导零.除了小时字段外,你可以使用它们.'G'应为'H' - > Ymd\TH:i:s\Z. (3认同)