使用开始和结束时间之间的时间过滤数组或XML [英] Filter Array or XML with Time between Start and End Time

查看:87
本文介绍了使用开始和结束时间之间的时间过滤数组或XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an API call. The call returns back XML, I convert to XML into an array and then use json_encode to send back the response in a jsonp call. What I am doing is working but the API doesn't allow me to filter by a status, filter by start and end time and also make sure that the room is available.

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

It gets crazy with several jsponp calls. Again, what I have is working but I need to figure out a way to filter one more time with my results. I have returned the correct status and the correct date, but I still have to filter out times. If the time is currently 10:29am I don't want to show booked rooms that start at 10:30 am because it isn't booked until 10:30am. So, if my current time is 10:29 am I need to filter through my events in my array and only show events that have a start and end time that have the current time in between.

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

A booking from 10-11 am should stay in my array but a booking from 2-4 pm shouldn't show in my array. As I said the array is first a returned XML document, then an array, then JSON so technically I can use whatever works to filter. PHP to filter in XML, PHP to filter the array. Even Javascript onces that PHP file is ready by my ajax call.

这是我的数组,然后我把它变成JSON并返回。我过滤的时间应该是TimeEventStart和TimeEventEnd。我可以使用当前的Javascript时间或PHP日期函数时间。我可以过滤数组或使用类似XMLPath的东西进行过滤。不确定什么是最有效的并且效果最好。

Here is my array before I turn it into JSON and return. The times I filter should be TimeEventStart and TimeEventEnd. I can either use current Javascript time or PHP date function time. I can either filter the array or use something like XMLPath to filter. Not sure what is the most efficient and would work best.

$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"
            ],
            "RoomDescription":[
               "Room06"
            ],
            "TimeEventStart":[
               "2014-05-09T18:00:00"
            ],
            "TimeEventEnd":[
               "2014-05-09T20:00:00"
            ],
            "EventName":[
               "Rob Brown"
            ],
            "SetupTypeDescription":[
               "(none)"
            ],
            "ReservationID":[
               "142"
            ],
            "OpenTime":[
               "1900-01-01T00:00:00"
            ],
            "CloseTime":[
               "1900-01-01T00:00:00"
            ],
            "EventTypeDescription":[
               [

               ]
            ],
            "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"
            ],
            "TimeZone":[
               "ET"
            ],
            "RoomCode":[
               "R06"
            ],
            "Room":[
               "Room 6"
            ],
            "RoomID":[
               "34"
            ],
            "StatusID":[
               "3"
            ],
            "EventTypeID":[
               "0"
            ],
            "DateAdded":[
               "2014-05-09T14:58:55.71"
            ],
            "DateChanged":[
               "2014-05-09T14:58:55.71"
            ],
            "ChangedBy":[
               "Admin"
            ]
         },
         {
            "BookingDate":[
               "2014-05-09T00:00:00"
            ],
            "StartBookingDate":[
               "2014-05-09T00:00:00"
            ],
            "RoomDescription":[
               "Room06"
            ],
            "TimeEventStart":[
               "2014-05-09T11:00:00"
            ],
            "TimeEventEnd":[
               "2014-05-09T12:00:00"
            ],
            "EventName":[
               "Jimmy James"
            ],
            "SetupTypeDescription":[
               "(none)"
            ],
            "ReservationID":[
               "141"
            ],
            "OpenTime":[
               "1900-01-01T00:00:00"
            ],
            "CloseTime":[
               "1900-01-01T00:00:00"
            ],
            "EventTypeDescription":[
               [

               ]
            ],
            "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"
            ],
            "TimeZone":[
               "ET"
            ],
            "RoomCode":[
               "R06"
            ],
            "Room":[
               "Room 6"
            ],
            "RoomID":[
               "34"
            ],
            "StatusID":[
               "1"
            ],
            "EventTypeID":[
               "0"
            ],
            "DateAdded":[
               "2014-05-09T14:58:15.17"
            ],
            "DateChanged":[
               "2014-05-09T14:58:15.17"
            ],
            "ChangedBy":[
               "Admin"
            ]
         }
      ]
   }
}
;

我的数组是以XML格式从SOAP调用返回的。我正在获取XML并使用

My array is being returned from a SOAP call in XML. I am getting the XML back and using

$sxml = simplexml_load_string($xml)

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

Then I am using a function called xmlToArray to put it into the array posted.

$axml = xmlToArray($sxml);

这是我转换为数组然后转换为JSON之前的XML。

Here is the XML before I convert to an Array and then JSON.

<?xml version="1.0" encoding="utf-8"?><Bookings>
  <Data>
    <BookingDate>2014-05-13T00:00:00</BookingDate>
    <StartBookingDate>2014-05-13T00:00:00</StartBookingDate>
    <RoomDescription>Room 06</RoomDescription>
    <TimeEventStart>2014-05-13T10:00:00</TimeEventStart>
    <TimeEventEnd>2014-05-13T10:30:00</TimeEventEnd>
    <EventName>Jake Long</EventName>
    <ReservationID>159</ReservationID>
    <ClosedAllDay>false</ClosedAllDay>
    <OpenTime>1900-01-01T00:00:00</OpenTime>
    <CloseTime>1900-01-01T00:00:00</CloseTime>
    <BookingID>731</BookingID>
    <TimeBookingStart>2014-05-13T10:00:00</TimeBookingStart>
    <TimeBookingEnd>2014-05-13T10:30:00</TimeBookingEnd>
    <GMTStartTime>2014-05-13T14:00:00</GMTStartTime>
    <GMTEndTime>2014-05-13T14:30:00</GMTEndTime>
    <RoomCode>R09</RoomCode>
    <Room>Room 9</Room>
    <RoomID>37</RoomID>
    <StatusID>3</StatusID>
    <DateAdded>2014-05-13T08:38:22.36</DateAdded>
    <DateChanged>2014-05-13T08:38:22.36</DateChanged>
    </Data>
</Bookings>


推荐答案

好的,所以我写了一些PHP函数,来过滤你需要什么。

Okay, so I wrote some PHP functions, to filter out what you demand.

要比较php中的日期,最简单的方法是将它们转换为Unix时间。这就是第一个功能所做的。 Unix时间是自1970年1月1日以来经过的时间(以秒为单位)。转换它们可以获得优势只需使用<和>运算符进行两次比较。

To compare dates in php it is easiest to convert them to Unix time. That's what the first function does. Unix time is the time elapsed in seconds since January 1st, 1970. Converting them gives the advantage you can simply use < and > operators to compare two times.

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

The next function (nowBetween), looks if the current time is between the two times given through ($start and $end).

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

Then I took your JSON string (as we didn't really get to see the XML source when I wrote this post) and converted it to PHP arrays. We can be sure it are arrays, as the second parameter in json_decode is set to true.

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

Last but not least, I loop through all bookings in the "data" section of your JSON content, read the GMTStart and GMTEnd times and check if the current time is between them using our function. If not so, then I remove the booking from the $bookings array.

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

The result is only the bookings with the correct times stay. My output now was empty, but that's because your bookings date from 2014-05-09... When I adapted the time it worked.

我希望这个答案适合你的需要。

I hope this answer suits your needs.

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"],"RoomDescription":["Room06"],"TimeEventStart":["2014-05-09T18:00:00"],"TimeEventEnd":["2014-05-09T20:00:00"],"EventName":["Rob Brown"],"SetupTypeDescription":["(none)"],"ReservationID":["142"],"OpenTime":["1900-01-01T00:00:00"],"CloseTime":["1900-01-01T00:00:00"],"EventTypeDescription":[[]],"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"],"TimeZone":["ET"],"RoomCode":["R06"],"Room":["Room 6"],"RoomID":["34"],"StatusID":["3"],"EventTypeID":["0"],"DateAdded":["2014-05-09T14:58:55.71"],"DateChanged":["2014-05-09T14:58:55.71"],"ChangedBy":["Admin"]},{"BookingDate":["2014-05-9T00:00:00"],"StartBookingDate":["2014-05-09T00:00:00"],"RoomDescription":["Room06"],"TimeEventStart":["2014-05-09T11:00:00"],"TimeEventEnd":["2014-05-09T12:00:00"],"EventName":["Jimmy James"],"SetupTypeDescription":["(none)"],"ReservationID":["141"],"OpenTime":["1900-01-01T00:00:00"],"CloseTime":["1900-01-01T00:00:00"],"EventTypeDescription":[[]],"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"],"TimeZone":["ET"],"RoomCode":["R06"],"Room":["Room 6"],"RoomID":["34"],"StatusID":["1"],"EventTypeID":["0"],"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]);
    }
}

这篇关于使用开始和结束时间之间的时间过滤数组或XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆