【问题标题】:Filter array on date with php用php过滤日期数组
【发布时间】:2015-02-26 12:05:34
【问题描述】:

我需要一些帮助来过滤日期数组。

下面的查询将打印一个包含所有条目的表格。但我不想过滤日期。 例如,如果今天是“2014 年 30 月 12 日”,我想显示 2014 年 12 月 30 日和“较新”的所有条目,而不是“2014 年 30 月 12 日”的第一个条目。

“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 让我走上了正轨。我知道有更多更好的方法来处理这个问题,但对我来说它可以正常工作。

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

【问题讨论】:

    标签: php arrays oracle date filter


    【解决方案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
    

    【讨论】:

    • 注意:请尽量避免将日期存储为字符串
    • @RitchieTomasouw 您是否更改了此查询中的任何内容
    • 你能发布新的查询来调试吗
    • @RitchieTomasouw 您应该将此作为问题发布,这肯定有一些我无法解决的结构性错误,如果您将其作为问题发布,可能其他人可以帮助您
    • @RitchieTomasouw 真的很抱歉,我没有注意到标签。我对 Oracle 数据库一无所知
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      使用 array_filter 函数。我不知道您的数据到底长什么样,但您或许可以根据以下示例弄清楚如何操作:

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

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

      【讨论】:

      • 您好,感谢您的评论。我把这段代码放在哪里?在 foreach 中?
      • 补充片段'foreach ($row as $item) { echo "" 。 ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") 。 "\n";' with $filtered = array_filter($row, function($date) use ($startDate, $endDate) { return strtotime($item) >= strtotime($startDate) && strtotime($item)
      猜你喜欢
      • 1970-01-01
      • 2020-06-25
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      • 1970-01-01
      • 2021-05-25
      • 2021-12-10
      • 2017-12-30
      相关资源
      最近更新 更多