We help IT Professionals succeed at work.

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

Bruce Gust
Bruce Gust asked
on
181 Views
Last Modified: 2018-12-02
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

David FavorFractional CTO
CERTIFIED EXPERT
Distinguished Expert 2019

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)
CERTIFIED EXPERT
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)
CERTIFIED EXPERT
Most Valuable Expert 2015
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
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.