The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Can't get checkdate to work!
Discuss Can't get checkdate to work! in the PHP Development forum on Dev Shed. Can't get checkdate to work! PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 30th, 2012, 10:22 PM
|
 |
A Change of Season
|
|
|
|
|
Can't get checkdate to work!
Hello;
I am calling the checkdate function and send an invalid value as argument (04-30w-2013), but it returns true for some reason! I wonder why! Thanks
PHP Code:
/*The code below prints:
04-30w-2013
Good Date */
public function valid_date($date)
{
$date_format = explode('-',$date);
echo $date_format['1']."-".$date_format['2']."-".$date_format['0'];
if(checkdate($date_format['1'],$date_format['2'],$date_format['0']))
{
echo "Good Date";
return true;
}
else
{
echo "Bad Date";
return false;
}
}
|

November 30th, 2012, 10:45 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
checkdate() only validates the numbers: 1 <= month <= 12, 1 <= day <= number of days in that month, and something with the year. It doesn't validate the data type.
Regex the whole string or get the components and ctype_digit them, then checkdate() once you know that you actually have real numbers to use.
|

November 30th, 2012, 10:55 PM
|
 |
A Change of Season
|
|
|
|
Quote: | Originally Posted by requinix checkdate() only validates the numbers: 1 <= month <= 12, 1 <= day <= number of days in that month, and something with the year. It doesn't validate the data type.
Regex the whole string or get the components and ctype_digit them, then checkdate() once you know that you actually have real numbers to use. | Thank you requinix. That's not good. Bad php. They can improve this.
|

November 30th, 2012, 11:14 PM
|
 |
A Change of Season
|
|
|
|
Quote: | Originally Posted by requinix checkdate() only validates the numbers: 1 <= month <= 12, 1 <= day <= number of days in that month, and something with the year. It doesn't validate the data type.
Regex the whole string or get the components and ctype_digit them, then checkdate() once you know that you actually have real numbers to use. | How about something like this:
PHP Code:
public function valid_date($date)
{
$date_format = explode('-',$date);
if(!is_numeric($date_format['0']) || $date_format['0']<date('Y') || $date_format['0']>strtotime("+2 years"))
{
$invalid_year=true;
}
if(!is_numeric($date_format['1']) || $date_format['1']<1 || $date_format['0']>12)
{
$invalid_month=true;
}
if(!is_numeric($date_format['2']) || $date_format['2']<1 || $date_format['0']>31)
{
$invalid_day=true;
}
if(!checkdate($date_format['1'],$date_format['2'],$date_format['0']))
{
$invalid_date=true;
}
if($invalid_year==true || $invalid_month==true || $invalid_day==true || $invalid_date==true)
{
return false;
}
}
|

December 1st, 2012, 02:31 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
Quote: | Originally Posted by zxcvbnm Thank you requinix. That's not good. Bad php. They can improve this. |
Why should they change it to suit you? The function is already clearly documented to take three integer arguments. Its purpose is to validate a potential date, not your string inputs.
Quote: | Originally Posted by zxcvbnm How about something like this: |
A little long-winded but sure, if that works for you.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|