Jump to content

[SOLVED] need help comparing dates


boo_lolly

Recommended Posts

i am writing a form validation script and i need help with a date field. users will enter in a date from a javascript datePicker in the form of MM/DD/YYYY. i already have regex validation to handle this, however, i need another feature to validate this date. the date entered cannot be a day before the current date. i'm not sure if you can do a less than or greater than with two dates, and how you get the date function to interpret a string date. really i have no idea where to go. can someone help me get started?

Link to comment
Share on other sites

Hey,

I am fairly new to PHP, but I have completed a few date comparison projects as of late.

I usually pick the dates apart into $current_month, $current_day, $current_year variables.  I do the same with desired month, date year.  Then I use if statements to compare the two and throw errors accordingly. See below:

 

<?PHP
//Lets breakdown the Current Date.  If you are using a different date structure, modify the substr accordingly.  
//If you need more help on substr, search PHP substr on google and click the first link.
$current_date = date("mdy");
    //These three lines pull the year, date and month into seperate variables to work with.
    $current_m = substr($current_date,0,2);
    $current_d = substr($current_date,2,2);
    $current_y = substr($current_date,4,2);

//Lets breakdown the Target Date  I am replacing the / with nothing because it is less confusing and the numbers 
//are the same as above.
$target_date = "12/20/07";
$target_date = trim(str_replace("/","",$target_date));
     $target_m = substr($target_date,0,2);
     $target_d = substr($target_date,2,2);
     $target_y = substr($target_date,4,2);

//Now your months, days, and years are broken into seperate variables.  This can be accomplished with arrays too 
//but this is easier if your not familiar with arrays.

//Lets set some error messages to tell us why we are being rejected!
$error_y = "You have selected a year in the past.";
$error_m= "You have selected a month in the past.";
$error_d = "You have selected a day in the past.";

//Lets test to see if the target date is before the current date.
if($target_y > $current_y){

    if($target_m > $current_m){

        if($target_d > $current_d){

            echo "SUCCESS! You have selected a date that is not today or before today but in the future!";

        }else{
            echo $error_d;
        }
    }else{
        echo $error_m;
    }
}else{
    echo $error_y;
}
?>

 

Let me know if this helps. ;)

Link to comment
Share on other sites

yeah that's what i figured. since i have a javascript datePicker and regex, i can validate the date that is inputted no problem, once the date format has been validated, i can then validate the user inputed date is NOT before the current date like this:

<?php
    $currentdate = date('m/d/Y');
    list($cm, $cd, $cy) = split('/', $currentdate);
    list($um, $ud, $uy) = split('/', $_POST['userinputdate']);

    if ($um < $cm) { return $error; }
    if ($ud < $cd) { return $error; }
    if ($uy < $cy) { return $error; }
?>

 

but as you mentioned in your post, that won't work because a month may be before the current month, but the year may be after, therefore it will be valid. so nested if statements are necessary, but your code is wrong. the logic is a little off (i think). i'm working on it right now. i'll get back when it works.

Link to comment
Share on other sites

There are a hundred ways you can do this, so it's up to you to pick one and run with it.

 

You can use your date stamp as formatted (MM/DD/YYYY) as the argument in strtotime(), which will result in a Unix timestamp. You can then use date() and mktime() to literally do any comparison you could possibly dream up. I would suggest following those links and becoming educated on how to use those time functions.

 

PhREEEk

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.