我需要一些关于在日期过滤数组的帮助。
下面的查询将打印一个包含所有条目的表格。但我不想过滤日期。 例如,今天是' 30-12-2014'我希望显示2014年12月30日的所有条目和"更新的"而不是从第一个年龄较大的那个条目' 30-12-2014'。
" appointment.start_time"是一个varchar2,输出如下:2014-11-28T15:35:00
在$ stid中我修剪了" appointment.start_time"使用substr所以输出就像" 2014-11-28"。
但是如何将过滤器放入?
这样的东西?
foreach ($row as $item)
if ($item->dateitem >= $startDate &&
$item->dateitem <= $endDate)
$newarray[] = $item;
你能帮我吗?
亲切的问候, 里奇
----------------------------------------------- ----查询php -------------------------------------------- --------------
$stid = oci_parse($conn, "select
customer.first_name as VOORNAAM,
customer.last_name as ACHTERNAAM,
substr(appointment.start_time,1,10) as DATUM,
substr(appointment.start_time,12,10) as STARTTIJD,
service_definitions.external_name as PRODUCT,
appointment.resource_name as AGENDA
from appointment
inner join appointment_customer on appointment.id = appointment_customer.appointmentjpa_id inner join customer on customer.id = appointment_customer.customerids
inner join appointment_service on appointment_service.appointment_id = appointment.id inner join service_definitions on service_definitions.id = appointment_service.service_id order by appointment.start_time asc ");
oci_execute($stid);
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
echo " < tr > \n ";
foreach ($row as $item)
{
echo "< td >" . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . "< /td >\n";
}
echo "< /tr >\n";
&#13;
------------------------------使用QUERY解决-------------- --------------------
大家好,我通过在查询中添加以下内容来解决我的问题:
where substr(appointment.start_time,1,10) >= TO_CHAR(SYSDATE, 'YYYY-MM-DD')
@krishna让我走上正轨。我知道有更多更好的方法可以解决这个问题,但对我来说,它的工作正常。
谢谢大家的帮助。 问候 里奇
答案 0 :(得分:1)
像这样更改您的查询
SELECT
customer.first_name as VOORNAAM,
customer.last_name as ACHTERNAAM,
substr(appointment.start_time,1,10) as DATUM,
substr(appointment.start_time,12,10) as STARTTIJD,
service_definitions.external_name as PRODUCT
, appointment.resource_name as AGENDA
from appointment where
CONCAT(Year( STR_TO_DATE( appointment.start_time, '%Y-%m-%d %H:%i:%S' ) ),'-',Month( STR_TO_DATE( appointment.start_time, '%Y-%m-%d %H:%i:%S' ) ),'-',Day( STR_TO_DATE( appointment.start_time, '%Y-%m-%d %H:%i:%S' ) )) > '2014-12-30' order by appointment.start_time asc
答案 1 :(得分:0)
您需要使用strtotime()函数首先将日期转换为unixtimestamp然后进行比较。
如下所示:
$row = array('30-12-2014','29-12-2014','28-12-2014','27-12-2014','26-12-2014','25-12-2014');
$startDate = '26-12-2014';
$endDate = '28-12-2014';
foreach ($row as $item)
if ( strtotime($item) >= strtotime($startDate) && strtotime($item) <= strtotime($endDate))
$newarray[] = $item;
print_r($newarray);
答案 2 :(得分:0)
使用array_filter函数。我不知道您的数据究竟是什么样子,但您可以根据以下示例找出如何执行此操作:
$arr = [strtotime('now'), strtotime('now-1'), strtotime('now+1')]
array_filter($arr, function($elem) { return $elem > 1419935201; })
其中1419935201是strtotime('now');
的值