Raised This Month: $51 Target: $400
 12% 

[SM] convert date to days


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ecca
Sexy Santa
Join Date: Jan 2011
Old 12-21-2011 , 17:57   [SM] convert date to days
Reply With Quote #1

Hello, well as you know i want to convert a date to days left until that date

An exampel

PHP Code:
2011-12-22 
to


PHP Code:
2011-12-24 
Is
PHP Code:
2 Days 
how do i convert it like that?
__________________
ecca is offline
mcpan313
Senior Member
Join Date: Mar 2010
Old 12-21-2011 , 18:51   Re: [SM] convert date to days
Reply With Quote #2

PHP Code:
stock ConvertDate(String:date1[], String:date2[])
{
    
ReplaceString(date1strlen(date1)+1"-"""); // replace 2011-12-22 to 20111222
    
ReplaceString(date2strlen(date2)+1"-""");

    
decl String:strYear[2][5], String:strMonth[2][3];
    
strcopy(strYear[0], sizeof(strYear[]), date1); // strYear[0] = "2011"
    
strcopy(strYear[1], sizeof(strYear[]), date2); 

    
ReplaceString(date1strlen(date1)+1strYear[0], ""); // date1 = "1222"
    
ReplaceString(date2strlen(date2)+1strYear[1], ""); 

    
strcopy(strMonth[0], sizeof(strMonth[]), date1); // strMonth[0] = "12"
    
strcopy(strMonth[1], sizeof(strMonth[]), date2); 

    
ReplaceString(date1strlen(date1)+1strMonth[0], ""); // date1 = "22" 
    
ReplaceString(date2strlen(date2)+1strMonth[1], "");

    if (!
strlen(date1)) strcopy(date1sizeof(strMonth[]), strMonth[0]); // if month == day, exampel "20110808"
    
if (!strlen(date2)) strcopy(date2sizeof(strMonth[]), strMonth[1]);

    
// (year1 - year2)*365 + (month1 - month2)*30 + (day1 - day2)  
    
return (StringToInt(strYear[0]) - StringToInt(strYear[1])) * 365 + (StringToInt(strMonth[0]) - StringToInt(strMonth[1])) * 30 + (StringToInt(date1) - StringToInt(date2));


Last edited by mcpan313; 12-22-2011 at 20:43. Reason: bug fix
mcpan313 is offline
Send a message via MSN to mcpan313
ecca
Sexy Santa
Join Date: Jan 2011
Old 12-22-2011 , 09:57   Re: [SM] convert date to days
Reply With Quote #3

Ok really dont understand that code to 100% so can you give me an example how to use it to?
__________________
ecca is offline
mcpan313
Senior Member
Join Date: Mar 2010
Old 12-22-2011 , 20:45   Re: [SM] convert date to days
Reply With Quote #4

Quote:
Originally Posted by ecca View Post
Ok really dont understand that code to 100% so can you give me an example how to use it to?
OK. example:
PHP Code:
public OnPluginStart()
{
    
decl String:date1[]="2011-12-24";
    
decl String:date2[]="2011-12-22";
    
    
LogMessage("ConverDate: %s to %s is %d"date1date2ConvertDate(date1date2));
}

stock ConvertDate(String:date1[], String:date2[])
{
    
ReplaceString(date1strlen(date1)+1"-"""); // replace 2011-12-22 to 20111222
    
ReplaceString(date2strlen(date2)+1"-""");

    
decl String:strYear[2][5], String:strMonth[2][3];
    
strcopy(strYear[0], sizeof(strYear[]), date1); // strYear[0] = "2011"
    
strcopy(strYear[1], sizeof(strYear[]), date2);

    
ReplaceString(date1strlen(date1)+1strYear[0], ""); // date1 = "1222"
    
ReplaceString(date2strlen(date2)+1strYear[1], "");

    
strcopy(strMonth[0], sizeof(strMonth[]), date1); // strMonth[0] = "12"
    
strcopy(strMonth[1], sizeof(strMonth[]), date2);

    
ReplaceString(date1strlen(date1)+1strMonth[0], ""); // date1 = "22" 
    
ReplaceString(date2strlen(date2)+1strMonth[1], "");

    if (!
strlen(date1)) strcopy(date1sizeof(strMonth[]), strMonth[0]); // if month == day, exampel "20110808"
    
if (!strlen(date2)) strcopy(date2sizeof(strMonth[]), strMonth[1]);

    
// (year1 - year2)*365 + (month1 - month2)*30 + (day1 - day2) 
    
return (StringToInt(strYear[0]) - StringToInt(strYear[1])) * 365 + (StringToInt(strMonth[0]) - StringToInt(strMonth[1])) * 30 + (StringToInt(date1) - StringToInt(date2));

result:
PHP Code:
L 12/23/2011 09:43:13: [new.smxConverDate24 to 22 is 2 
__________________
sorry, for my poor english.
mcpan313 is offline
Send a message via MSN to mcpan313
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:59.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode