1

I am trying to display lines from a file based on a date range (ex: get today + next 7 days based on date field). I'm not familiar with PHP. I have the following to read the file and create an array from the contents:

$txt_file    = file_get_contents('shipping_dates.txt');
$rows        = explode("\n", $txt_file);
array_shift($rows);

foreach($rows as $row => $data)
{
    //get row data
    $row_data = explode(',', $data);

    $info[$row]['Order_Date']           = $row_data[0];
    $info[$row]['Free_Shipping']        = $row_data[1];
    $info[$row]['Rush_Order']           = $row_data[2];

    //display data
    echo 'Row ' . $row . ' Order_Date: ' . $info[$row]['Order_Date'] . '<br />';
    echo 'Row ' . $row . ' Free_Shipping: ' . $info[$row]['Free_Shipping'] . '<br />';
    echo 'Row ' . $row . ' Rush_Order: ' . $info[$row]['Rush_Order'] . '<br />';

}

This works great - Now I need to grab the individual lines based on a date range and display only the lines within the range only, not the entire contents for each of these 3 options.

5
  • Could you post a small sample of the text file format so that I can try to run your code? Commented Feb 14, 2018 at 18:07
  • Looks like this is CSV. Take a look at fgetcsv Commented Feb 14, 2018 at 18:09
  • I suspect you would be working then with $info[$row]['Order_Date']? Is that date in any normal format, or a string, or... ? You may want to looke into DateTime object, and the createFromFormat function to then do date operations with it. php.net/manual/en/datetime.createfromformat.php Commented Feb 14, 2018 at 18:09
  • 2
    That typo has been corrected however it is waiting for peer review. Commented Feb 14, 2018 at 18:13
  • Here's a part of the text file I'm importing: Order_Date,Free_Shipping,Rush_Order 1/3/2018,1/17/2018,1/10/2018 1/4/2018,1/18/2018,1/11/2018 1/5/2018,1/19/2018,1/12/2018 1/6/2018,1/22/2018,1/15/2018 1/7/2018,1/22/2018,1/15/2018 Commented Feb 15, 2018 at 20:35

1 Answer 1

1

I would do:

Assuming your desired date range is only for 'entire days' without considering the hours.

Get the date range in unix TimeStamp format. Example for January 2018. You have to manually add " 00:00:00" and " 23:59:59" so you cover the full days.

Before your foreach loop:

$filter_date_from = '2018-01-01';
$filter_date_to = '2018-01-31';

$date_from = $filter_date_from.' 00:00:00'; // add begin of the day
$date_to = $filter_date_to.' 23:59:59'; // add end of the day

$date_from_ts = strtotime($date_from); // convert to unix timestamp
$date_to_ts = strtotime($date_to);

Then inside the loop you would check this, once you have $info[$row]['Order_Date'] stuffed:

// convert the order date to unix timestamp
$order_date_ts = strtotime($info[$row]['Order_Date']);

// just compare
if ($order_date_ts >= $date_from_ts && $order_date_ts <= $date_to_ts) {
    // you want the CSV line because it is IN the date range
}

With this solution you don't care if the Order_Date includes hours, minutes and/or seconds, or none of them (strtotime will assume 00:00:00 if none are provided).

Please note that the dates/datetimes must be in a format that the php function "strtotime" understands, like Y-m-d. See documentation for strtotime if you get weird results.

Not tested but let me know if it works for you...

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you - that's got me going in the right direction! After 20 years, I'm switching languages to PHP - mostly just learning the new syntax but I'm stubborn and don't want to learn a new language :-)
One last thing - how can I format the date from the line below to get something like Friday, March 2? - echo 'Row ' . $row . ' Order_Date: ' . $info[$row]['Order_Date'] . '<br />';
You can check the php "date" function, or strftime

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.