Icebergness Posted May 10, 2012 Share Posted May 10, 2012 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?? Cheers Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 10, 2012 Share Posted May 10, 2012 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 10, 2012 Share Posted May 10, 2012 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"; } ?> Quote Link to comment Share on other sites More sharing options...
Icebergness Posted May 10, 2012 Author Share Posted May 10, 2012 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.