Form Validation: Comparing two dates.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
J_Mo
Forum Newbie
Posts: 10
Joined: Mon Aug 09, 2010 7:02 pm

Form Validation: Comparing two dates.

Post by J_Mo »

Hi,

I have an issue with date comparison in my form validation. Basically, my 'Payday' field must always be less than my 'Nextpayday' field. I can't get it to validate.

I have:

Code: Select all

$date1 = date("dd/mm/yyyy");
$date2 = date("dd/mm/yyyy");

if((strtotime($date1['Payday']) - strtotime($date2['Nextpayday'])) < 0){
        $error_hash['Nextpayday']="Next payday must be later than payday'";
            return false;
}
Obviously this is wrong, can someone help?
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: Form Validation: Comparing two dates.

Post by PHPHorizons »

Hello J_Mo,

Well, you have defined $date1 and $date2 exactly the same way. Therefore, they have the same exact value.

Code: Select all

$date1 = date("dd/mm/yyyy");
$date2 = date("dd/mm/yyyy");
Secondly, date() returns a date string. But you are treating $date1 and $date2 as an array. There are not arrays in your code, they are strings.

Cheers
J_Mo
Forum Newbie
Posts: 10
Joined: Mon Aug 09, 2010 7:02 pm

Re: Form Validation: Comparing two dates.

Post by J_Mo »

Dont worry...I'v have sorted it myself

Code: Select all

$payday = $formars['Payday'];
$nextpayday = $formars['Nextpayday'];

if ($payday > $nextpayday) {
$error_hash['Nextpayday']="Next Payday can't be before Payday<style>#nextpayday_error{color: #f00;padding-right: 15px;background: url(../img/warning.gif) no-repeat 100% 50%;}</style>";
            return false;
}
Thanks
Post Reply