Quote:
Originally Posted by Lordzy
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.
|
Okay i write one but didn't test and seems to work
PHP код:
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;
}
Seems like it can optimise more what you think?