0

I have in issue with this code, I'm reusing it from a different script, it is reading from an xml file and converting the date/time from a node. The date in the node is as follows which is the only difference to the original script:

<od>10:15:41 01/03/13</od>

I thought I had this modified correctly but it isn't working:

$_date=$record->getElementsByTagName("od");
$_date=((!empty($_date))?$_date->item(0)->nodeValue:"");
if(strpos($_date,".")!==false)
{
    $_date=substr($_date,0,strpos($_date,"."));
}
$_date=date("H:i:s m/d/Y",strtotime($_date));
$_date.=(trim($_date)!="")?"Z":"";
xmlrpc_set_type($_date, 'datetime');

Any help is much appreciated.

4

2 に答える 2

1

日付/時刻10:15:41 01/03/13の形式が無効です

DateTime::createFromFormat代わりに使用してください。

于 2013-02-23T13:26:41.570 に答える
0

strftimeY-m-d H:i:s明確であるため、フォーマットで問題なく動作します。

一方、 とH:i:s m/d/y解釈されるため、 と混同されH:i:s d/m/Yます。2013 年 2 月 3 日という日付について考えてみましょう。m/d/y は 2 月 3 日を示し、d/m/Y は 3 月 2 日を示します。

つまり、毎回正しい日付を取得するには、より具体的にする必要があります。date_create_from_format('H:i:s m/d/y', $_date)指定された日付が実際に「H:i:sm/d/y」形式である場合、正しい日付に対応する DateTime オブジェクトが返されます。

// Retrieve the date string
$_date=$record->getElementsByTagName("od");
$_date=((!empty($_date))?$_date->item(0)->nodeValue:"");

// Standardize it
$_date = get_date( $_date );
$_date .= (trim($_date) != "") ? "Z" : "";
xmlrpc_set_type($_date, 'datetime');



function get_date( $rawDate ) {

    // Clean date string
    if(strpos($rawDate,".")!==false) {
        $rawDate=substr($rawDate,0,strpos($rawDate,"."));
    }


    // Attempt converting from m/d/y AND m/d/Y formats
    $date = date_create_from_format('H:i:s m/d/y', $rawDate);
    if( false === $date ) $date = date_create_from_format('H:i:s m/d/Y', $rawDate);

    if( !empty($date) ) {
        return $date->format('H:i:s m/d/Y'); // Convert the date to a string again
    }


    // If neither works, try using strtotime instead
    $date = @strtotime($rawDate);
    $date = !empty($date) ? date('H:i:s m/d/y', $date) : false;


    return $date;
}

それが役立つことを願っています!

于 2013-02-23T13:26:23.760 に答える