Remove dates older than today from sting?

Hi,
If I have a string of dates in format "dd/mm/yyyy" , is there an easy way to clear dates that are older than today?

$mydates = '01/02/2015, 03/03/2015, 01/04/2015, 06/04/2015, 10/04/2015, 14/04/2015, 15/04/2015, 17/04/2015, 23/04/2015';

Thanks
sabecsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Duy PhamFreelance IT ConsultantCommented:
I think the only option is split source string into array of dates, and then remove the date elements where were in the past, then re-join the remaining ones (greater than today) to build a new string of dates again.

$arrDates = explode(", ", $stringOfDates);
$today = date("d/m/Y");

for ($x = sizeof($arrDates) - 1; $x >= 0; $x--) {
    $aDate = date_create_from_format("d/m/Y", $arrDates[$x]);
    if ($aDate < $today) {
        array_splice($arrDates, $x, 1);
    }
}

$stringOfDates = implode(", ", $arrDates);

Open in new window

0
Chris StanyonWebDevCommented:
Have a look at this:

$mydates = '01/02/2015, 03/03/2015, 01/04/2015, 06/04/2015, 10/04/2015, 14/04/2015, 15/04/2015, 17/04/2015, 23/04/2015';

// Create an array by splitting your string on the comma
$dateArray = explode(',', $mydates);

// Create a new, filtered array
$filteredDates = array_filter($dateArray, function($item) {
	// Create a date from each item
	$date = DateTime::createFromFormat('d/m/Y', trim($item));
	$today = new DateTime();
	// Keep the date if it's today or later
	return ($date >= $today);
});

// Take a look at the new array
var_dump($filteredDates);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
There are many ways to convert and process dates.  Some of them are shown in this article:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

The right way to store dates for any "internal" use is in the ISO-8601 format.  We reformat them for external purposes, like display, or when data interchange makes it necessary to comply with a non-standard specification, but otherwise it's 100% ISO standard, 100% of the time.

To find the ISO standard date of yesterday, you can do this do this:

$yesterday = date('c', strtotime('Yesterday'));

ISO standard dates can be compared, sorted, etc.  Some of the standard formats include times as well as dates.

Please see: http://iconoun.com/demo/temp_sabecs.php
<?php // demo/temp_sabecs.php

/**
 * See http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28649407.html
 */
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA FROM THE POST AT EE IS IN THE WRONG FORMAT
$mydates = '01/02/2015, 03/03/2015, 01/04/2015, 06/04/2015, 10/04/2015, 14/04/2015, 15/04/2015, 17/04/2015, 23/04/2015';

// BRING THE DATES UP TO STANDARD FORMAT
$arr = explode(',', $mydates);
foreach ($arr as $k => $v)
{
    $v = str_replace('/', '-', $v);
    $standard[] = date('Y-m-d', strtotime(trim($v)));
}

// SHOW THE STANDARDIZED DATES
var_dump($standard);

// TODAY
$today = date('Y-m-d');

// SEPARATE THE SETS
$old = [];
$now = [];
$fut = [];
foreach ($standard as $date)
{
    if ($date  < $today) $old[] = $date;
    if ($date  > $today) $fut[] = $date;
    if ($date == $today) $now[] = $date;
}

var_dump($old, $now, $fut);

Open in new window

Best regards, ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.