php – 在开始和结束时间之间使用时间过滤数组或XML

前端之家收集整理的这篇文章主要介绍了php – 在开始和结束时间之间使用时间过滤数组或XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个API调用.该调用返回XML,我将XML转换为数组,然后使用json_encode在jsonp调用中发回响应.我正在做的是工作,但API不允许我按状态过滤,按开始和结束时间过滤,并确保房间可用.

几个jsponp调用让它变得疯狂.再一次,我所拥有的是工作,但我需要找到一种方法来再次使用我的结果过滤.我已经返回了正确的状态和正确的日期,但我仍然需要过滤掉时间.如果时间目前是上午10点29分,我不想显示从上午10点半开始预订的房间,因为直到上午10点半才开始预订.因此,如果我当前的时间是上午10:29,我需要过滤我的数组中的事件,并且只显示具有当前时间的开始和结束时间的事件.

从上午10点到11点的预订应该留在我的阵列中,但是从下午2-4点开始的预订不应该出现在我的阵列中.正如我所说,数组首先是一个返回的XML文档,然后是一个数组,然后是JSON,所以在技术上我可以使用任何工作来过滤. PHP以XML格式过滤,PHP过滤数组.甚至Javascript也可以通过我的ajax调用来准备PHP文件.

在我将其转换为JSON并返回之前,这是我的数组.我过滤的时间应该是TimeEventStart和TimeEventEnd.我可以使用当前的Javascript时间或PHP日期函数时间.我可以过滤数组或使用类似XMLPath的东西进行过滤.不确定什么是最有效的,最好的.

$axml = Array{
   "Bookings":{
      "Data":[
         {
            "BookingDate":[
               "2014-05-09T00:00:00"
            ],"RoomDescription":[
               "Room06"
            ],"TimeEventStart":[
               "2014-05-09T14:00:00"
            ],"TimeEventEnd":[
               "2014-05-09T14:30:00"
            ],"EventName":[
               "Jake Long"
            ],"SetupTypeDescription":[
               "(none)"
            ],"ReservationID":[
               "137"
            ],"OpenTime":[
               "1900-01-01T00:00:00"
            ],"CloseTime":[
               "1900-01-01T00:00:00"
            ],"EventTypeDescription":[
               [

               ]
            ],"BookingID":[
               "709"
            ],"TimeBookingStart":[
               "2014-05-09T14:00:00"
            ],"TimeBookingEnd":[
               "2014-05-09T14:30:00"
            ],"GMTStartTime":[
               "2014-05-09T18:00:00"
            ],"GMTEndTime":[
               "2014-05-09T18:30:00"
            ],"TimeZone":[
               "ET"
            ],"RoomCode":[
               "R06"
            ],"Room":[
               "Room 6"
            ],"RoomID":[
               "34"
            ],"StatusID":[
               "3"
            ],"EventTypeID":[
               "0"
            ],"DateAdded":[
               "2014-05-09T13:47:29.087"
            ],"DateChanged":[
               "2014-05-09T13:47:29.087"
            ],"ChangedBy":[
               "Admin"
            ]
         },{
            "BookingDate":[
               "2014-05-09T00:00:00"
            ],"TimeEventStart":[
               "2014-05-09T18:00:00"
            ],"TimeEventEnd":[
               "2014-05-09T20:00:00"
            ],"EventName":[
               "Rob Brown"
            ],"ReservationID":[
               "142"
            ],"BookingID":[
               "714"
            ],"TimeBookingStart":[
               "2014-05-09T18:00:00"
            ],"TimeBookingEnd":[
               "2014-05-09T20:00:00"
            ],"GMTStartTime":[
               "2014-05-09T22:00:00"
            ],"GMTEndTime":[
               "2014-05-10T00:00:00"
            ],"DateAdded":[
               "2014-05-09T14:58:55.71"
            ],"DateChanged":[
               "2014-05-09T14:58:55.71"
            ],"StartBookingDate":[
               "2014-05-09T00:00:00"
            ],"TimeEventStart":[
               "2014-05-09T11:00:00"
            ],"TimeEventEnd":[
               "2014-05-09T12:00:00"
            ],"EventName":[
               "Jimmy James"
            ],"ReservationID":[
               "141"
            ],"BookingID":[
               "713"
            ],"TimeBookingStart":[
               "2014-05-09T11:00:00"
            ],"TimeBookingEnd":[
               "2014-05-09T12:00:00"
            ],"GMTStartTime":[
               "2014-05-09T15:00:00"
            ],"GMTEndTime":[
               "2014-05-09T16:00:00"
            ],"StatusID":[
               "1"
            ],"DateAdded":[
               "2014-05-09T14:58:15.17"
            ],"DateChanged":[
               "2014-05-09T14:58:15.17"
            ],"ChangedBy":[
               "Admin"
            ]
         }
      ]
   }
}
;

我的数组是从XML中的SOAP调用返回的.我正在获取XML并使用

$sxml = simplexml_load_string($xml)

然后我使用一个名为xmlToArray的函数将它放入发布的数组中.

$axml = xmlToArray($sxml);

在转换为Array然后转换为JSON之前,这是XML.

最佳答案
好的,所以我编写了一些PHP函数来过滤掉你的需求.

要比较PHP中的日期,最简单的方法是将它们转换为Unix时间.这就是第一个功能所做的. Unix time是自1970年1月1日以来经过的时间(以秒为单位).转换它们的优势在于您只需使用<和>操作符比较两次.

下一个函数(nowBetween)查看当前时间是否在给定的两次($start和$end)之间.

然后我拿了你的JSON字符串(因为我在撰写这篇文章时没有真正看到XML源代码)并将其转换为PHP数组.我们可以确定它是数组,因为json_decode中的第二个参数设置为true.

最后但并非最不重要的是,我遍历JSON内容的“数据”部分中的所有预订,阅读GMTStart和GMTEnd时间,并使用我们的函数检查当前时间是否在它们之间.如果不是这样,那么我从$bookings数组中删除预订.

结果只剩下正确时间的预订.我的输出现在是空的,但那是因为你的预订日期是2014-05-09 …当我调整它的工作时间.

我希望这个答案能满足你的需求.

function convertDateTime($datetime) {
    //FORMAT IS date("YYYY-MM-DDThh:mm:ss")
    $datetime = str_replace("T"," ",$datetime);
    return date("U",strtotime($datetime));
}
function nowBetween($start,$end) {
    $now = gmdate("U");
    $start = convertDateTime($start);
    $end = convertDateTime($end);
    if ($start <= $now && $end > $now) 
    { 
        return true;
    }
    else
    {
        return false;
    }
}

$bookings = '{"Bookings":{"Data":[{"BookingDate":["2014-05-09T00:00:00"],"RoomDescription":["Room06"],"TimeEventStart":["2014-05-09T14:00:00"],"TimeEventEnd":["2014-05-09T14:30:00"],"EventName":["Jake Long"],"SetupTypeDescription":["(none)"],"ReservationID":["137"],"OpenTime":["1900-01-01T00:00:00"],"CloseTime":["1900-01-01T00:00:00"],"EventTypeDescription":[[]],"BookingID":["709"],"TimeBookingStart":["2014-05-09T14:00:00"],"TimeBookingEnd":["2014-05-09T14:30:00"],"GMTStartTime":["2014-05-09T18:00:00"],"GMTEndTime":["2014-05-09T18:30:00"],"TimeZone":["ET"],"RoomCode":["R06"],"Room":["Room 6"],"RoomID":["34"],"StatusID":["3"],"EventTypeID":["0"],"DateAdded":["2014-05-09T13:47:29.087"],"DateChanged":["2014-05-09T13:47:29.087"],"ChangedBy":["Admin"]},{"BookingDate":["2014-05-09T00:00:00"],"TimeEventStart":["2014-05-09T18:00:00"],"TimeEventEnd":["2014-05-09T20:00:00"],"EventName":["Rob Brown"],"ReservationID":["142"],"BookingID":["714"],"TimeBookingStart":["2014-05-09T18:00:00"],"TimeBookingEnd":["2014-05-09T20:00:00"],"GMTStartTime":["2014-05-09T22:00:00"],"GMTEndTime":["2014-05-10T00:00:00"],"DateAdded":["2014-05-09T14:58:55.71"],"DateChanged":["2014-05-09T14:58:55.71"],{"BookingDate":["2014-05-9T00:00:00"],"StartBookingDate":["2014-05-09T00:00:00"],"TimeEventStart":["2014-05-09T11:00:00"],"TimeEventEnd":["2014-05-09T12:00:00"],"EventName":["Jimmy James"],"ReservationID":["141"],"BookingID":["713"],"TimeBookingStart":["2014-05-09T11:00:00"],"TimeBookingEnd":["2014-05-09T12:00:00"],"GMTStartTime":["2014-05-09T15:00:00"],"GMTEndTime":["2014-05-09T16:00:00"],"StatusID":["1"],"DateAdded":["2014-05-09T14:58:15.17"],"DateChanged":["2014-05-09T14:58:15.17"],"ChangedBy":["Admin"]}]}}';
$bookings = json_decode($bookings,true);


foreach ($bookings["Bookings"]["Data"] as $index=>$arrContent) 
{
    if (!nowBetween($arrContent["GMTStartTime"][0],$arrContent["GMTEndTime"][0])) 
    {
        unset($bookings["Bookings"]["Data"][$index]);
    }
}
原文链接:https://www.f2er.com/jquery/428155.html

猜你在找的jQuery相关文章