Another question dateTime

I currently have a date in this format

2010-03-03 10:39:18

which is a field TIMESTAMPin MySQL. I need to specify a date in this format for the Solr search engine:

1995-12-31T23:59:59Z

Here are some texts from their website about dates:

Solr expects dates to be in UTC when indexing. The format for this date field is 1995-12-31T23: 59: 59Z, and this is a more limited form of the canonical representation of the date http://www.w3.org/TR/xmlschema-2/#dateTime . The ending "Z" indicates UTC time and is mandatory. Optional fractional seconds allowed: 1995-12-31T23: 59: 59.999Z All other components are required.

I was given this code from another Q here on SO, but it does not work. Solr complains about an "invalid time string":

$solr_date = date('c', (strtotime($date_from_mysql)); // doesn't work

$solr_date, Z, , . .

+2
6

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;
    }
+5

, , / MySQL - . , .

MySQL, , , CONVERT_TZ() UTC DATE_FORMAT(), . :.

SELECT CURRENT_TIMESTAMP,
    CONVERT_TZ(CURRENT_TIMESTAMP, '+01:00', '+00:00'),
    DATE_FORMAT(CONVERT_TZ(CURRENT_TIMESTAMP, '+01:00', '+00:00'), '%Y-%m-%dT%H:%i:%sZ')

, , TIMESTAMP ​​. , .

PHP, date() , :

<?php
$ts = strtotime($date_from_mysql);
$solr_date date('Y-m-d', $ts) . 'T' . date('H:i:s', $ts) . 'Z';
?>
+2

gmdate('Y-m-d\TH:i:s\Z', $time);

+2

, :

$to = date('c', strtotime($to)).'.000Z';
0

:

$datetime = new DateTime('2010-01-19 00:00:00');
echo str_replace('+00:00', 'Z', $datetime->format('c'));

0

PHP:

(2010-03-03 10:39:18) mktime.

$solr_date = date('c', mktime($year,$month,$date, $h, $m , $s)); # it will work

-1

All Articles