stock checkdate(d,m,y)
{
new daysinmonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
new valid = 0;
// leap year checking, if ok add 29 days to february
if(y % 400 == 0 || (y % 100 != 0 && y % 4 == 0))
daysinmonth[1]=29;
// days in month checking
if(m < 13)
{
if(d <= daysinmonth[m-1])
{
valid=1;
}
}
return valid;
}
bool:isValidDate(str[])
{
new count;
new daysinmonth[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
new bool:legit;
for(new i,j=strlen(str);i<j;i++)
if(str[i]=='/')count++;
if(count != 2) return false;
new d,y,m;
if(sscanf(str, "p</>iii", d, m, y))return false;
if(d < 1 && d > 31) return false;
if(m < 1 && m > 12) return false;
if(y % 400 == 0 || (y % 100 != 0 && y % 4 == 0))
daysinmonth[1]=29;
if (m<13)
{
if( d <= daysinmonth[m-1] )
legit=true;
}
if (!legit) return false;
return true;
}
|
Okay i re edited my code that i given before it will do the job as it is most common method / algorithm to validate a date in c programs.
PHP код:
|
|
Not every date separators are "/" sign and also not every countries use (DD/MM/YYYY) format. Maybe you could an extra argument which helps to knows the date separator and format, thereby improving the function.
|

enum
{
DDMMYY,
DDYYMM,
MMDDYY,
MMYYDD,
YYDDMM,
YYMMDD
}
bool:isValidDate(str[],form=DDMMYY,seperator='/')
{
new count;
new daysinmonth[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
new bool:legit;
for(new i,j=strlen(str);i<j;i++)
if(str[i]==seperator)count++;
if(count != 2) return false;
new d,y,m,string[9];
format(string,sizeof(string),"p<%c>iii",seperator);
if(form == DDMMYY)
if(sscanf(str, string,d, m, y)) return false;
else if(form == DDYYMM)
if(sscanf(str, string,d, y, m)) return false;
else if(form == MMDDYY)
if(sscanf(str, string,m, d, y)) return false;
else if(form == MMYYDD)
if(sscanf(str, string,m, y, d)) return false;
else if(form == YYDDMM)
if(sscanf(str, string,y, d, m)) return false;
else if(form == YYMMDD)
if(sscanf(str, string,y, m, d)) return false;
if(d < 1 && d > 31) return false;
if(m < 1 && m > 12) return false;
if(y % 400 == 0 || (y % 100 != 0 && y % 4 == 0))
daysinmonth[1]=29;
if (m<13)
{
if( d <= daysinmonth[m-1] )
legit=true;
}
if (!legit) return false;
return true;
}
|
I too can use 01 02 03 04 05 06 07 08 09 in numbers, i mean sscanf understand it like 1 2 3 4 5 6 7 8 9 ?
|
main()
{
if(isValidDate("12/12/12",DDMMYY,'/') ) printf("Valid date"); //actually what now i passed is not required as it is default values check with others options too.
else printf("Not valid");
}
|
Not every date separators are "/" sign and also not every countries use (DD/MM/YYYY) format. Maybe you could an extra argument which helps to knows the date separator and format, thereby improving the function.
|