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

Bruce Gust
Bruce Gust used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
David FavorFractional CTO
Distinguished Expert 2018

Commented:
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)
Most Valuable Expert 2015

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
Owner (Aidellio)
Most Valuable Expert 2015
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

Bruce GustPHP Developer

Author

Commented:
Thanks, guys! And yes, Rob, in this case I'm expecting all the duplicates to be listed right next to one another.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial