比较日期只是字符串的一部分

时间:2014-10-29 16:14:50

标签: php

我有两个字符串(从XML文件中读取),说' 2014-10-17 T11:17:44'和' 2014-10-17 T16:19:15'。我想仅使用日期部分(没有时间)来比较它们。我已经尝试了......下面的脚本

if(file_exists($xmlFullFilename)) {
        if($entryTimeNode->format("Y-m-d") = $entryTimeNode->format("Y-m-d")){
                    ( appends to the exisiting file ....)
                    $xmlDoc = new DomDocument();
                    $tmp = split(" ", $entryTime);
                $dateString = $tmp[0] . "T" . $tmp[1];
                $entryTimeNode = $xmlDoc->createElement("EntryTime", $dateString);
}
 else {
        //create a new xml file .
}}

XML文件

<Incidents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Count="2" Date="2014-10-24" Time="12:05:39" FileName="2014-10-24_Brook_Retail_Park.xml">
      <Incident>
      <EntryTime>2014-10-17T11:17:44</EntryTime>
      </Incident>
    <Incident>
<EntryTime>2014-10-17T16:19:15</EntryTime>
    </Incident></Incidents>

1 个答案:

答案 0 :(得分:0)

我的解决方案是

$date = "2014-10-17T11:17:44";
$date2 = "2014-10-17T16:19:15";

$date = new DateTime($date);
$date2 = new DateTime($date2);


$diff = $date->diff($date2);

if((int)$diff->format('%y%m%d')==0)
{
    // Dates are equal
}