# Robson » Little Red Book » Date check

## Chapter 4 - Question 5

Design a program that will accept as input three numbers representing a date in the form day, month, year between 1900 and 2000 and output either OK, if the date is acceptable (represents an actual date), or "NO", if it does not.

### Solution 1

C++.

Example usage:

`C:\code>checkdate 31 4 1982NOC:\code>checkdate 31 3 1982OKC:\code>checkdate 29 2 1944OK`
`#include <stdio.h>`
`#include <stdlib.h>`
` `
`using namespace std;`
` `
`int main(int argc, char **argv)`
`{`
`    if (argc != 4)`
`        printf("Usage: checkdate [day] [month] [year]");`
`    else`
`    {`
`        // days per month`
`        int iDaysPerMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };`
`        // check if it's a leap year`
`        if (!(atoi(argv) % 4))`
`            // set max days in february to 29`
`            iDaysPerMonth = 29;`
` `
`        // check the date`
`        if (atoi(argv) >= 1900 && atoi(argv) <= 2000 && // year`
`            atoi(argv) >= 1 && atoi(argv) <= 12 && // month`
`            atoi(argv) >= 1 && atoi(argv) <= iDaysPerMonth[atoi(argv)-1]) // day`
`            printf("OK");`
`        else`
`            printf("NO");`
`    }`
`}`

### Solution 2

The PHP solution is very simple because the checkdate function can handle all the checking.

`<?`
`    // generate a random year between the specified amounts`
`    \$year = mt_rand(1900, 2000);`
`    // generate a day and month`
`    // allows invalid values so it can be checked`
`    \$month = mt_rand(0, 14);`
`    \$day = mt_rand(0, 35);`
`    `
`    // show the user the randomly generated date`
`    echo \$day . '/' . \$month . '/' . \$year . ' = ';`
`    // check if it's valid`
`    if (checkdate(\$month, \$day, \$year))`
`        // show the user that it is`
`        echo 'OK';`
`    else`
`        // it's not, tell the user`
`        echo 'NO';`
`?>`

Which produces

6/5/1982 = OK

## Log

• May 7, 2005 - Added solution 1 and 2.
© 2004-19 robson | cc unless stated