|
 |
checkdate (PHP 3, PHP 4, PHP 5) checkdate -- Проверяет правильность даты по грегорианскому календарю Описаниеbool checkdate ( int month, int day, int year )
Возвращает TRUE если дата, заданная аргументами, является правильной;
иначе возвращает FALSE.
Дата считается правильной, если:
год в диапазоне от 1 до 32767 включительно
месяц в диапазоне от 1 до 12 включительно
day является допустимым номером дня для
месяца, заданного аргументом
month, принимая во внимание,что
year может задавать високосный год.
Пример 1. Пример использования функции checkdate()
<?php
var_dump(checkdate(12, 31, 2000));
var_dump(checkdate(2, 29, 2001));
?>
|
Приведенный выше пример выведет:
|
См.также описание функций mktime() и strtotime().
checkdate
moklet
20-Mar-2006 05:34
ANd now it accepts date formats like yyyy/mm/dd as well
<?php
function datecheck($date, $yearepsilon=5000, $format='ymd') {
$date=str_replace("/", "-", $date);
$format = strtolower($format);
if (count($datebits=explode('-',$date))!=3) return false;
$year = intval($datebits[strpos($format, 'y')]);
$month = intval($datebits[strpos($format, 'm')]);
$day = intval($datebits[strpos($format, 'd')]);
if ((abs($year-date('Y'))>$yearepsilon) || ($month<1) || ($month>12) || ($day<1) ||
(($month==2) && ($day>28+(!($year%4))-(!($year%100))+(!($year%400)))) ||
($day>30+(($month>7)^($month&1)))) return false; return array(
'year' => $year,
'month' => $month,
'day' => $day
);
}
?>
</Life>.org
05-Mar-2006 01:07
Taking the comments of Hightechguy into account, here is a version with variable date formatting:
<?php
function datecheck($date, $yearepsilon=5000, $format='ymd') {
$format = strtolower($format);
if (count($datebits=explode('-',$date))!=3) return false;
$year = intval($datebits[strpos($format, 'y')]);
$month = intval($datebits[strpos($format, 'm')]);
$day = intval($datebits[strpos($format, 'd')]);
if ((abs($year-date('Y'))>$yearepsilon) || ($month<1) || ($month>12) || ($day<1) ||
(($month==2) && ($day>28+(!($year%4))-(!($year%100))+(!($year%400)))) ||
($day>30+(($month>7)^($month&1)))) return false; return array(
'year' => $year,
'month' => $month,
'day' => $day
);
}
?>
Hightechguy
01-Feb-2006 02:17
As an addition to the function contributed by </Life>.org:
You may want to use different date format "YYYY-MM-DD" like "2006-02-01", because
a) it's default for mySQL and
b) relatively easily retrieved from MSSQL if you use CONVERT function with style=ODBC canonical, which gives yyyy-mm-dd hh:mi:ss(24h) style
Finally function looks like:
<?php
function dateCheck($date, $yearepsilon=5000) { if (count($datebits=explode('-',$date))!=3) return false;
$year = intval($datebits[0]);
$month = intval($datebits[1]);
$day = intval($datebits[2]);
if ((abs($year-date('Y'))>$yearepsilon) || ($month<1) || ($month>12) || ($day<1) ||
(($month==2) && ($day>28+(!($year%4))-(!($year%100))+(!($year%400)))) ||
($day>30+(($month>7)^($month&1)))) return false; return checkdate($month,$day,$year );
}
?>
</Life>.org
20-Aug-2005 09:35
The function of phil@philmo contains a few errors...
I corrected them as far as I found them and tried to improve the code.
<?php
function datecheck($date, $yearepsilon=5000) {
if (count($datebits=explode('-',$date))!=3) return false;
$month = intval($datebits[0]);
$day = intval($datebits[1]);
$year = intval($datebits[2]);
if ((abs($year-date('Y'))>$yearepsilon) || ($month<1) || ($month>12) || ($day<1) ||
(($month==2) && ($day>28+(!($year%4))-(!($year%100))+(!($year%400)))) ||
($day>30+(($month>7)^($month&1)))) return false; return array(
'year' => $year,
'month' => $month,
'day' => $day
);
}
?>
Matt Browne
21-Jul-2005 10:58
Here's a date validation snippet I wrote, since I wasn't able to find one that was flexible enough. It accepts dates in m-d-y format (2- or 4-digit year) and both dashes and slashes as separators.
$strDate = $yourDate;
$isValid = false;
if (ereg('^([0-9]{1,2})[-,/]([0-9]{1,2})[-,/](([0-9]{2})|([0-9]{4}))$', $strDate)) {
$dateArr = split('[-,/]', $param->value);
$m=$dateArr[0]; $d=$dateArr[1]; $y=$dateArr[2];
$isValid = checkdate($m, $d, $y);
}
if (!$isValid) {
// take appropriate action
}
// then, to obtain the timestamp
$date = mktime(null,null,null,$m,$d,$y);
Zoe Blade
20-Jul-2005 06:31
If you need to check a MySQL format date, here's a function to do it:
/*
* Check a MySQL (ISO 8601 based) format date
*
* The first part splits the date up into its component parts, checking that
* they appear to be roughly OK. The regex specifically looks for:
* four digits between 1000 and 9999, -, two digits between 01 and 12, -,
* two digits between 01 and 31).
*
* The second part makes sure that the date is definitely valid (taking into
* account leap years).
*
* See http://dev.mysql.com/doc/mysql/en/datetime.html for details on the MySQL
* date format.
*/
function checkmysqldate($isodate)
{
if (preg_match("/^([123456789][[:digit:]]{3})-(0[1-9]|1[012])-
(0[1-9]|[12][[:digit:]]|3[01])$/", $isodate, $date_part) && checkdate($date_part[2], $date_part[3], $date_part[1]))
{
return true;
}
else
{
return false;
}
}
(The form made me insert a character return between the hyphen and open bracket in the regex line due to wrapping. Just join those two lines together to get the code to work!)
martin a pavel
19-Jul-2005 02:14
<?php
function DaysInMonth ( $Year, $MonthInYear )
{
if ( in_array ( $MonthInYear, array ( 1, 3, 5, 7, 8, 10, 12 ) ) )
return 31;
if ( in_array ( $MonthInYear, array ( 4, 6, 9, 11 ) ) )
return 30;
if ( $MonthInYear == 2 )
return ( checkdate ( 2, 29, $Year ) ) ? 29 : 28;
return false;
}
?>
cristianDOTzuddas [SPAM] gmail DOT com
09-Jul-2005 02:38
This function returns TRUE if the passed string contains a valid MySQL date.
The allowed format is YYYY-MM-DD and the supported range is from '1000-01-01' to '9999-12-31' (MySQL limitations).
Here is the code:
http://www.codeflower.com/index.php?a=showCode&id=36
Ex.:
is_valid_mysql_date('2005-05-21'); <-- TRUE
is_valid_mysql_date('2005-5-30'); <-- FALSE
| |