Jump to content

Date > A future date


Icebergness

Recommended Posts

Hi,

 

This one has been driving me up the wall, so hopefully some kind person can help me.

 

I'm trying to make a validation script. $creststartdate comes in from a form as a UK formatted date (d/m/Y), and if $creststartdate is more than a month ahead of today, then it gets rejected. here's the code I have now...

 


            $today = date("m/d/y");
            $onemonth = strtotime ('+1 month', strtotime($today));
            $nextmonth = date ('d/m/Y', $onemonth);
            
            $csd = date("m/d/y", $creststartdate);
            $strcsd = strtotime($csd);
            $newdate = date ('d/m/Y', $strcsd);

            if ($newdate > $nextmonth)
                {
                    $creststartdatefailed = "The Crest Start Date cannot be more than 1 month in the future.";
                    $creststartdatevalid = "NO";
                }

 

As it stands, this version of the code means that nothing is getting rejected.

 

Please help??  :shrug:

 

Cheers

Link to comment
Share on other sites

Compare them as timestamps, not strings.You have  $onemonth (which is one month from today) and $strcsd (which is a unix timestamp of the date). Compare those two.

 

Also go through and echo all your variables to see what they hold, make sure they are what you expect.

Link to comment
Share on other sites

try

<?php
  $creststartdate = '15/6/2012';                 // UK date, unfortunate a format
                                                 // not recognised by strtotime()
  date_default_timezone_set('GMT');
                                                 
  list($d, $m, $y) = explode('/', $creststartdate);
  if (mktime(0,0,0,$m,$d,$y) > strtotime('+ 30 days')) {
    echo "Beyond 30 days";
  } else {
    echo "OK";
  }
?>

Link to comment
Share on other sites

try

<?php
  $creststartdate = '15/6/2012';                 // UK date, unfortunate a format
                                                 // not recognised by strtotime()
  date_default_timezone_set('GMT');
                                                 
  list($d, $m, $y) = explode('/', $creststartdate);
  if (mktime(0,0,0,$m,$d,$y) > strtotime('+ 30 days')) {
    echo "Beyond 30 days";
  } else {
    echo "OK";
  }
?>

 

Barand,

 

Thank you!! I'm still learning a lot of PHP and haven't dabbled in mktime yet. I'd looked at it, but wasn't sure how to use it, but that worked perfectly. I can sleep tonight! :)

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.