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
https://forums.phpfreaks.com/topic/262354-date-a-future-date/
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
https://forums.phpfreaks.com/topic/262354-date-a-future-date/#findComment-1344485
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
https://forums.phpfreaks.com/topic/262354-date-a-future-date/#findComment-1344488
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
https://forums.phpfreaks.com/topic/262354-date-a-future-date/#findComment-1344576
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.