示例#1
0
    if (!checkdate($month, $day, $year)) {
        return false;
    }
    $now = new DateTime();
    // Right now vs. then below:
    $then_formatted = sprintf("%d-%d-%d", $year, $month, $day);
    $then = DateTime::createFromFormat("Y-n-j|", $then_formatted);
    $age = $now->diff($then);
    if ($age->y < $min_age || $age->y > $max_age) {
        return FALSE;
    } else {
        return TRUE;
    }
}
// check December 3, 1974
if (checkbirthdate(12, 3, 1974)) {
    print "You may use this web site. <br>";
} else {
    print "You are too young (or old, you should be dead!!) to proceed. <br>";
}
// Parsing Dates and times from strings
$a = strtotime('march 10');
// defaults to the current year
echo $a . "<br>";
$b = strtotime('last thursday');
echo $b . "<br>";
$c = strtotime('now + 3 months');
echo $c . "<br>";
$a = strtotime('now');
print date(DATE_RFC850, $a);
print "<br>";
示例#2
0
<?php

function checkbirthdate($month, $day, $year)
{
    $min_age = 18;
    $max_age = 122;
    if (!checkdate($month, $day, $year)) {
        return false;
    }
    $now = new DateTime();
    $then_formatted = sprintf("%d-%d-%d", $year, $month, $day);
    $then = DateTime::createFromFormat("Y-n-j|", $then_formatted);
    $age = $now->diff($then);
    if ($age->y < $min_age || $age->y > $max_age) {
        return false;
    } else {
        return true;
    }
}
if (checkbirthdate(12, 3, 200)) {
    print "You may use this web site.";
} else {
    print "You are too young (or too old!!) to proceed.";
}