• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 64
  • Last Modified:

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
0
sabecs
Asked:
sabecs
2 Solutions
 
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 StanyonCommented:
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Tackle projects and never again get stuck behind a technical roadblock.
Join Now