Avatar of Bruce Gust
Bruce GustFlag 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
Avatar of David Favor
David Favor
Flag of United States of America image

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

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
Avatar of Rob
Rob
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Bruce Gust
Bruce Gust
Flag of United States of America image

ASKER

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

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo