我有一个API调用。 该调用返回XML,我将XML转换为数组,然后使用json_encode在jsonp调用中发回响应。 我正在做的是工作,但API不允许我按状态过滤,按开始和结束时间过滤,并确保房间可用。
几个jsponp调用让它变得疯狂。 再一次,我所拥有的是工作,但我需要找到一种方法来再次过滤我的结果。 我已经返回了正确的状态和正确的日期,但我仍然需要过滤掉时间。 如果时间目前是上午10点29分,我不想显示从上午10点30分开始预订的房间,因为直到上午10:30才预订。 因此,如果我当前的时间是上午10:29,我需要过滤我的数组中的事件,并且只显示具有当前时间的开始和结束时间的事件。
从上午10点到11点的预订应该留在我的arrays中,但是从下午2-4点开始的预订不应该出现在我的arrays中。 正如我所说,数组首先是一个返回的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" ], "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并使用
$sxml = simplexml_load_string($xml)
然后我使用一个名为xmlToArray的函数将它放入发布的数组中。
$axml = xmlToArray($sxml);
在转换为Array然后转换为JSON之前,这是XML。
2014-05-13T00:00:00 2014-05-13T00:00:00 Room 06 2014-05-13T10:00:00 2014-05-13T10:30:00 Jake Long 159 false 1900-01-01T00:00:00 1900-01-01T00:00:00 731 2014-05-13T10:00:00 2014-05-13T10:30:00 2014-05-13T14:00:00 2014-05-13T14:30:00 R09 Room 9 37 3 2014-05-13T08:38:22.36 2014-05-13T08:38:22.36
好的,所以我编写了一些PHP函数来过滤掉你的需求。
要比较php中的日期,最简单的方法是将它们转换为Unix时间。 这就是第一个function所做的。 Unix时间是自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"],"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]); } }
您可以使用Xpath过滤XML。 Xpath 1.0无法比较字符串,但您也可以注册PHP函数。
所以用PHP编写条件:
function isBetween($current, $min, $max) { $current = strtotime($current.'Z'); $min = strtotime($min.'Z'); $max = strtotime($max.'Z'); return $current >= $min && $current <= $max; }
创建DOM文档并加载XML。 创建Xpath实例注册PHP命名空间和函数。
$dom = new DOMDocument(); $dom->loadXml($xml); $xpath = new DOMXpath($dom); $xpath->registerNamespace("php", "https://php.net/xpath"); $xpath->registerPhpFunctions('isBetween');
定义一个Xpath表达式,该表达式使用该函数来过滤Data元素节点:
// use gmtime() for current time $now = strtotime('2014-05-13T14:00:00Z'); $expression = sprintf( '/Bookings/Data[ php:function("isBetween", "%s", string(GMTStartTime), string(GMTEndTime)) ]', gmdate('Ymd\TH:i:s', $now) );
使用Xpath表达式获取Data元素节点:
$result = []; foreach($xpath->evaluate($expression) as $dataNode) { $record = []; // convert the matching data into a record array foreach ($xpath->evaluate('*', $dataNode) as $childNode) { $record[$childNode->localName] = $childNode->nodeValue; } $result[] = $record; } var_dump($result);
演示: https : //eval.in/151732
据我了解,您将在<预订>中收到几条记录? 那么你有两个选择:如果记录数总是很大 – 超过几千个那么你应该使用XPath过滤数据之前将数据转换为数组 – 从XPath复制节点 – >查询结果到新文档并转换这个文档到数组。 如果记录数量很小,那么之后更容易转换它们 – 只需使用带有类似于此的回调的array_filter:
$filteredResult = array_filter($data, function($row) { // parse dates and compare them. });
你会得到过滤数组。
以上就是jQuery教程分享使用开始和结束时间之间的时间过滤数组或XML相关内容,想了解更多jQuery开发(异常处理)及jQuery教程关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/jquerytutorial/524412.html