0

I need some help with filtering an array on date.

The query below will print a table with all entry's ever. But i wan't to filter on date. If for example today is '30-12-2014' i want to show all entry's from 30-12-2014 and "newer" and not from the first entry thats older then '30-12-2014'.

The "appointment.start_time" is a varchar2 with an output like: 2014-11-28T15:35:00

In the $stid i trim the "appointment.start_time" with substr so the output is like "2014-11-28".

But how do i put the filter in?

Something like this?

foreach ($row as $item)
    if ($item->dateitem >= $startDate  &&  
        $item->dateitem <= $endDate)
            $newarray[] = $item;

Can you help me please?

Kind regards, Ritchie

--------------------------------------------------- query 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";

------------------------------ SOLVED WITH QUERY ----------------------------------

Hi there all, I solved my problem by adding the following in the query:

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

@krishna put me on the right track. I know that there are more and better ways to handle this but for me its working fine with this.

Thanks all for the help. Regards Ritchie

0

3 Answers 3

1

change your query like this

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
Sign up to request clarification or add additional context in comments.

6 Comments

Note: Please try to avoid storing dates as strings
@RitchieTomasouw did you changed any thing from this query
can you post the new query to debug
@RitchieTomasouw you should post this as question, this is surely having some structural error which i cant resolve it, may be others can help you if you post it as question
@RitchieTomasouw i am really sorry, i didn't notice the tag. I dont have idea about Oracle DB
|
0

You will need to use strtotime() function to first convert the date to unixtimestamp and then to compare them.

Like below:

$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);

Comments

0

Use array_filter function. I don't know how exactly your data looks, but you might be able to figure out how to do it based on the following example:

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

where 1419935201 is a value of strtotime('now');

2 Comments

Hi thanks for your comment. Where do i put this code? in the foreach ?
Replece the fragment 'foreach ($row as $item) { echo "< td >" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "&nbsp;") . "< /td >\n";' with $filtered = array_filter($row, function($date) use ($startDate, $endDate) { return strtotime($item) >= strtotime($startDate) && strtotime($item) <= strtotime($endDate) }); and the iterate over $filtered array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.