Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

Validate PHP date

I need a simple code which will validate if date is older date than today and if is in the right format.... like this...

date = now (dd.mm.yy)

If date area is empty....

Error... output your date is empty

If date format is not right (dd.mm.yy)

Error... you must provide date in the right format

If date is older than today....

Error... you cant pick older date than today

If everything is ok.....

Date = RightDate

Else Rightdate = ""


  • 4
  • 3
1 Solution
first of all you've txwo solutions : server-side (PHP) or client-side (jscript/javascript)

let's do it PHP-style as you posted this in the PHP area :D
(not tested)

function Delta2Dates($parmax,$parmin) { // en jours
  GLOBAL $dbHost, $dbName, $dbLogin, $dbPassword, $sess_osWin;
  $linkID=mysql_connect("$dbHost","$dbLogin","$dbPassword") or die ("bad connect".mysql_error());
  mysql_select_db($dbName,$linkID) or die ("bad select DB ".mysql_error());
  $query="select to_days('$parmax')-to_days('$parmin') as a;";
  $result=mysql_query($query,$linkID) or die ("bad query deltaDates ".mysql_error());
  return $locres;
} // Delta2Dates Integer Function

if (isset($_POST)) { // posted data present
  // get the date submitted
  $zarbi=explode('.',$locDate); // no need to typecast for integer comparison, PHP is "intelligent" enough
  // analyse it for validity
  $msg="date has to be in the past"; // important default value
  if (($zarbi[0]<=0)or($zarbi[0]>31)) {$msg='day invalid'; $ok=FALSE;} // day invalid
  if (($zarbi[1]<=0)or($zarbi[1]>12)) {$msg='month invalid'; $ok=FALSE;} // month invalid
  if (($zarbi[2]<='70)or($zarbi[0]>31)) {$msg='day invalid'; $ok=FALSE;} // day invalid
  // get now() and compare
  if (strlen($zarbi[2])<4) $zarbi[2]='19'.$zarbi[2]; // constraint
  $today=date("Y-m-d"); // MySql-compatible
  $yourtoday=date("d.m.y"); // your format DD.MM.YY
  $ok=($ok AND (Delta2Dates($today,$transformed)>=0));
  // proceed
  if (! $ok) { // display error line and set default date to today
    echo "error : $msg<BR><BR>";
  } // if not OK
} else { // no posted data, this is first entering :D
  $rightDate='DD.MM.YYYY'; // or whatever default value you want
} // if $_POST set

// here your FORM action=$PHP_SELF method=POST
// with echo "<INPUT type=text name=FDate value=$rightDate>";
note that you've to provide DB values, of else I've a solution not inviolving the MySql DB (but it would be a shame). In the same spirit, the testing of days between 1 and 31 and month(e)s between 1 and 12 may be replaced by a MySql-check that the date is valid. This would be cleaner.
bit shorter version without any sql connection needed

$today = getdate();
(int)$today = strtotime($today["mday"]." ".$today["month"]." ".$today["year"]);

/* lets assume you called your textbox "yourdate"
and  you link from some site to this one */

$rightDate = FALSE;

//validate POST method
if(!$_POST["yourdate"]) {
  echo "Your date is empty";
} else {
  //check first of right format valid is dd.mm.yy
    echo "Error... you must provide date in the right format";
  } else {
    $submitDate = explode(".",$_POST["yourdate"]);
    //lets make sure user doesn't overroll 31 days and 12 months
    if($submitDate[0] > 31 || $submitDate[1] > 12)
      DIE("Invalid Day or Month submitted.");
    (int)$submitDate = mktime(0,0,0,$submitDate[1],$submitDate[0],$submitDate[2]);
    if($submitDate < $today){
      echo "Error... you cant pick older date than today";
    } else {
      //just a 100% reverse check since giving in 31.02.03 exists also
      if(date("d.m.y",$submitDate) != $_POST["yourdate"])
        DIE("Invalid Timeformat.");
      echo $submitDate."<br>";
      echo "Thank you";
      $rightDate = $_POST["yourdate"];

//checked, valid, works
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

ah sorry, that echo $submitDate was just for testing purposes, just remove it :)

cabriocabrioAuthor Commented:
I had to modify a litle but BIG THANX!

//bljak  you rule ;)
please not that the bljak script :
-builds upon mine
-doesn't test the validity of the date (MySql as a perpetual calendar, very handy for this kind of stuff)
-uses alphabetical sorting on dates : this culd pose problems depending on the locale setting for date (huh huh huh)
-will accept days and months being 0
-uses an ereg_replace() that you'll never maintain nor understand again in 2 months.

My code is always the best solution possible given some constraints I impose myself (readability, understandable/expandable by other people, etc)

Good luck...
Dear VGR
as for the points 1 and 2 and 3 and 4 and 5
- noone said that he has mysql or any sql installed, (huh huh)
- it does have validate check, there is even a piece of code commented with //just a reverse check
- it converts all stuf into int
- will accept 0, but in reverse check it will throw an error (see point 2)
- there is no ereg_replace(); (only ereg() to match correct input)


I don't care. If he's happy with your stuff, then let's rock 'n' roll !  :)))

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now