使用 php 过滤日期数组


Filter array on date with php

我需要一些关于按日期过滤数组的帮助。

下面的查询将打印一个包含所有条目的表格。但我不想按日期过滤。例如,如果今天是"30-12-2014",我想显示 30-12-2014 和"较新"的所有条目,而不是来自第一个比"30-12-2014"更早的条目。

"appointment.start_time"是一个 varchar2,输出如下:2014-11-28T15:35:00

在$stid中,我用substr修剪了"appointment.start_time",因此输出类似于"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) : "&nbsp;") . "< /td >'n";
            }
            echo "< /tr >'n";

------------------------------

查询----------------------------------解决

大家好,我通过在查询中添加以下内容解决了我的问题:

where substr(appointment.start_time,1,10) >= TO_CHAR(SYSDATE, 'YYYY-MM-DD')

@krishna让我走上了正确的轨道。我知道有更多更好的方法来解决这个问题,但对我来说,它工作得很好。

谢谢大家的帮助。问候里奇

像这样更改查询

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

你需要使用 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);

使用array_filter函数。我不知道您的数据究竟是什么样子的,但您可以根据以下示例弄清楚如何做到这一点:

$arr = [strtotime('now'), strtotime('now-1'), strtotime('now+1')]
array_filter($arr, function($elem) { return $elem > 1419935201; })

其中1419935201是 strtotime('now')的值;