How can I get two dates to be compared to one another in the context of a loop?

How would I compare two dates in the context of a loop?

I've got a table full of duplicates. I've been able to craft a foreach loop that accurately retrieves and displays duplicate values. But then, when I've got two duplicates to consider, I need to compare the dates and be able to give the user the opportunity to click a button that will delete the date that is the oldest.

I'm coming up short.

Here's what I've been trying:

<?php 
$current_date="";
foreach($current_list as $current)
{
	if($current['ssn']==$current['ssn'])
	{
		//I've got a duplicate
		if($current_date<>"")
		{
			if($current_date>strtotime($current['date'])
			{
				echo "a rebel yell";
			}
			else
			{
				echo "say nothing";
			}
		}
	}
$current_ssn=$current['ssn'];	
$current_date=strtotime($current['date']);
}
?>

Open in new window


It's not working. I've tried several different configurations and I'm ooen to suggests.

Any ideas?
Bruce GustPHP DeveloperAsked:
Who is Participating?
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.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Easy way is to convert all your dates to seconds, then just do simple arithmetic + convert results to human readable form, if required.
RobOwner (Aidellio)Commented:
I can see a few issues with the code you've supplied... Did you redact much more of the code as that wouldn't run without errors for me.

The following will always return true
if($current['ssn']==$current['ssn'])

Open in new window


Did you mean one of them to be the variable $current_ssn ?
if($current['ssn']==$current['ssn'])

Your "if" statement was missing a closing right bracket:
if($current_date>strtotime($current['date']))

I'm working on a solution for you that i'll post soon
RobOwner (Aidellio)Commented:
This is what I have so far... does the sample data look right?  There is an issue though where you code would not pick up duplicates unless they were right next to each other... was that intentional?

<?php
error_reporting(E_ALL); 
// sample data
$current_list = array(
    array('ssn' => 'abc', 'date' => '2018-08-11'),
    array('ssn' => 'abc', 'date' => '2019-09-11'),
    array('ssn' => 'bcd', 'date' => '2018-07-11'),
    array('ssn' => 'cde', 'date' => '2018-06-11'),
    array('ssn' => 'def', 'date' => '2018-05-11'),
    array('ssn' => 'efg', 'date' => '2018-05-11'),
    array('ssn' => 'efg', 'date' => '2018-01-11'),
    array('ssn' => 'efg', 'date' => '2018-02-11'),
    array('ssn' => 'cde', 'date' => '2018-03-11'),
    array('ssn' => 'cde', 'date' => '2018-03-12'),
    array('ssn' => 'def', 'date' => '2018-03-13'),
    array('ssn' => 'fgh', 'date' => '2018-08-14'),
    array('ssn' => 'fgh', 'date' => '2018-08-15'),
    array('ssn' => 'abc', 'date' => '2018-08-16'),
    array('ssn' => 'bcd', 'date' => '2018-08-17'),
    array('ssn' => 'cde', 'date' => '2018-08-18'),
    array('ssn' => 'fgh', 'date' => '2018-08-19'),
    array('ssn' => 'abc', 'date' => '2018-09-21')
);

$current_date = "";
$current_ssn = "";
$days = 0;
echo "<pre>";
foreach ($current_list as $current) {
    if ($current_ssn == $current['ssn']) {
        //I've got a duplicate
        if ($current_date <> "") {
            $tmp_date = new DateTime($current['date']);
            $diff = $tmp_date->diff($current_date);
            print_r($diff);
            //if ($current_date > strtotime($current['date'])) {
            $days = ($diff->invert == 1) ? $diff->days*-1 : $diff-days*1;
            if ($days > 0) {
                echo "**a rebel yell<br/>";
            } else {
                echo "**say nothing<br/>";
            }
        }
    }
    $current_ssn = $current['ssn'];
    $current_date = new DateTime($current['date']); //strtotime($current['date']);
}
echo "</pre>";
?>

Open in new window

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
Bruce GustPHP DeveloperAuthor Commented:
Thanks, guys! And yes, Rob, in this case I'm expecting all the duplicates to be listed right next to one another.
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.