15.01.2017, 14:50
(
Последний раз редактировалось SyS; 19.01.2017 в 15:12.
)
isValidDate
Gist Link
Okay this function is created on the behalf of this discussion.It validates a date which is passed as string to the function.It doesn't need any delimiter or format to specify actually.The function will identify date,month and year automatically.
Parameters: str - date in string
Gist Link
Okay this function is created on the behalf of this discussion.It validates a date which is passed as string to the function.It doesn't need any delimiter or format to specify actually.The function will identify date,month and year automatically.
Parameters: str - date in string
PHP код:
bool:isValidDate(str[])//by Sreyas
{
new count,i,j;
new daysinmonth[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
new parts[3][5],Index_Beg;
new bool:legit;
while(str[i]!='\0')
{
if(str[i] < 48 || str[i] > 57)
{
count++;
strmid(parts[j], str, Index_Beg, i, sizeof(parts[]));
Index_Beg = i+1;
j++;
}
i++;
if(str[i]=='\0')
{
strmid(parts[j], str, Index_Beg, i, sizeof(parts[]));
}
}
if(count != 2) return false;
new number[3];
for(i=0;i<3;i++)
{
number[i] = strval(parts[i]);
if(number[i] == 0)return false;
}
new y,m,d;
y = m = d = -1;
for(i=0;i<3;i++)
{
if(number[i]>31)
{
if(y != -1) return false;
y = i;
}
}
for(i=0;i<3;i++)
{
if(i == y) continue;
if(number[i] < 13 && number[i] > 0)
{
if(m == -1)
{
m = i;
}
else
{
d = i;
}
}
}
if(y==-1)
{
for(i=0;i<3;i++)
{
if(i==d||i==m) continue;
y = i;
}
}
if(d==-1)
{
for(i=0;i<3;i++)
{
if(i==y||i==m) continue;
d = i;
}
}
if(m==-1)
{
for(i=0;i<3;i++)
{
if(i==y||i==d) continue;
m = i;
}
}
if(number[y] % 400 == 0 || (number[y] % 100 != 0 && number[y] % 4 == 0))
daysinmonth[1]=29;
if( number[m]<13 && number[d] <= daysinmonth[number[m]-1])
legit=true;
if (!legit) return false;
return true;
}
NOTE: More faster function and one using sscanf is posted by Lordzy below. So i prefer you to use that instead of this as it proved to be more slow.