is date exist.
#1

Hi,

How to check is specific date, is exist?, like there is months, when days is 28 29 30 31, for ex if month have 30 days, but if date with 31 specified, then that date don't exist, but how to check any date i want?
Reply
#2

I haven't tried it out, but you can use something like this function

PHP код:
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;
} 
Reply
#3

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.Ported version
PHP код:
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;
} 
Reply
#4

Quote:
Originally Posted by Sreyas
Посмотреть сообщение
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 код:
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;
} 
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.
Reply
#5

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?
Reply
#6

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 ?
Reply
#7

Quote:
Originally Posted by MerryDeer
Посмотреть сообщение
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 ?
yes you can and also is that code working? I can't test it right now.
Reply
#8

How to test to say it's working?
Reply
#9

Quote:
Originally Posted by MerryDeer
Посмотреть сообщение
How to test to say it's working?
just add this function to your script and call it in main()
like this
PHP код:
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");
} 
it should print "Valid date" in console.
Reply
#10

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.
Doesn't really matter because you should set a standard for input. If I input 12/01/2017 the system can't know if I mean 12 January or 1 December. The internationally unambiguous ISO 8601 date format (YYYY-MM-DD) is probably the best.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)