Avatar of Bruce Gust
Bruce Gust
Flag for United States of America asked on

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?
PHP

Avatar of undefined
Last Comment
Bruce Gust

8/22/2022 - Mon
David Favor

Easy way is to convert all your dates to seconds, then just do simple arithmetic + convert results to human readable form, if required.
Rob

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
ASKER CERTIFIED SOLUTION
Rob

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Bruce Gust

ASKER
Thanks, guys! And yes, Rob, in this case I'm expecting all the duplicates to be listed right next to one another.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck