PYTHON checkdate

is this article helpful?
|
Python replacement for PHP's checkdate [ edit | history ]
def checkdate(m, d, y):
    import datetime
    try:
        m, d, y = map(int, (m, d, y))
        datetime.date(y, m, d)
        return True
    except ValueError:
        return False


Using:

checkdate(12, 10, 2000)
# True

checkdate("01", "01", "2000")
# True

checkdate(13, 10, 2000)
# False

checkdate("12", "32", "2000")
# False

PHP checkdate

PHP original manual for checkdate [ show | php.net ]

checkdate

(PHP 4, PHP 5)

checkdateValidate a Gregorian date

Description

bool checkdate ( int $month , int $day , int $year )

Checks the validity of the date formed by the arguments. A date is considered valid if each parameter is properly defined.

Parameters

month

The month is between 1 and 12 inclusive.

day

The day is within the allowed number of days for the given month . Leap year s are taken into consideration.

year

The year is between 1 and 32767 inclusive.

Return Values

Returns TRUE if the date given is valid; otherwise returns FALSE.

Examples

Example #1 checkdate() example

<?php
var_dump
(checkdate(12312000));
var_dump(checkdate(2292001));
?>

The above example will output:

bool(true)
bool(false)

See Also

  • mktime() - Get Unix timestamp for a date
  • strtotime() - Parse about any English textual datetime description into a Unix timestamp