Hi All,
Appologies if this is in the wrong area!
Ive trying to create an 'inline' in post event count down timer, ideally using BBcode so forum users can create countdown timers to certain events.
What i have so far pretty much works however some of the maths isn't quite right, and so while most dates show up correctly some don't and I'm at my wits end trying to work out where ive gone wrong! Can someone have a look and point me in the right direction - to be clear - the code I'm using is not my own.
Im on PHPBB 3.2.0
BBCode Usage
HTML Replacement
Help Line
testing this on todays date on my forum (Todays date been: 2018/03/10: YYYY/MM/DD) i get the following results (ive shown the event code infront of each for ease)
As you can see some dates work perfectly some don't - any help would be gratefully received
Appologies if this is in the wrong area!
Ive trying to create an 'inline' in post event count down timer, ideally using BBcode so forum users can create countdown timers to certain events.
What i have so far pretty much works however some of the maths isn't quite right, and so while most dates show up correctly some don't and I'm at my wits end trying to work out where ive gone wrong! Can someone have a look and point me in the right direction - to be clear - the code I'm using is not my own.
Im on PHPBB 3.2.0
BBCode Usage
Code Select
[event={NUMBER1}/{NUMBER2}/{NUMBER3}]{TEXT}[/event]
HTML Replacement
Code Select
<script language="JavaScript">
// February has 29 days in any year evenly divisible by four, EXCEPT for centurial years which are not also divisible by 400.
function daysInFebruary (year) {
return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
var Eventtext = "<strong>{TEXT}</strong>" ;
var Future = new Date('{NUMBER1}/{NUMBER2}/{NUMBER3}');
var Today = new Date();
var daysinmonth = { 1:31, 2:(daysInFebruary(Today.getYear())), 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31 };
var F_Year = Future.getYear();
var F_Month = Future.getMonth();
var F_Day = Future.getDate();
var years = 0;
var months = 0;
var days = 0;
while( F_Year != Today.getYear() ) { if ( F_Year < Today.getYear() ) { F_Year++; years--; } else { F_Year--; years++; F_Month++;} }
while( F_Month != Today.getMonth() ) { if ( years < 0 ) { months = Today.getMonth()-F_Month; break;} else { if ( F_Month < Today.getMonth() ) { F_Month++; months--; } else { F_Month--; months++; } } }
while( F_Day != Today.getDate() ) { if ( years < 0 ) { days = Today.getDate()-F_Day; break;} else { if ( F_Day < Today.getDate() ) { F_Day++; days--; } else { F_Day--; days++; } } }
if ( years > 0 ) {
if ( days < 0 ) { days = daysinmonth[F_Month]-Math.abs(days-1); months--; }
if ( months < 0 ) { months = 12-Math.abs(months-1); years--; }
}
if ( !years && !months && !days ) { document.write(" Today is " + Eventtext); }
else if ( !years && !months && days == 1 ) { document.write(" Tomorrow is " + Eventtext); }
else if ( !years && !months && days == -1 ) { document.write(" Yesterday was " + Eventtext); }
else {
var word_year = ( Math.abs(years) == 1 ) ? " year, " : " years, ";
var word_month = ( Math.abs(months) == 1) ? " month, " : " months, ";
var word_day = ( Math.abs(days) == 1 ) ? " day" : " days";
var word_elapsed= ( Future > Today ) ? " until " : " since ";
var Output = (years != 0) ? Math.abs(years) + word_year: '' ;
Output += (months != 0) ? Math.abs(months) + word_month : '' ;
Output += (days != 0) ? Math.abs(days) + word_day : '' ;
document.write(Output + word_elapsed + Eventtext); }
</script>
Help Line
Code Select
[event=DATE]NAME THE EVENT[/event] Example : [event=2008/09/22]Next workday[/event]
testing this on todays date on my forum (Todays date been: 2018/03/10: YYYY/MM/DD) i get the following results (ive shown the event code infront of each for ease)
Code Select
[event=2018/03/09][/event]
Produces: Yesterday - correctCode Select
[event=2018/03/10][/event]
Produces: Today - correctCode Select
[event=2018/03/11][/event]
Produces: Tomorrow - correct Code Select
[event=2019/03/09][/event]
Produces: 1 year, 26 days - wrongCode Select
[event=2019/03/10][/event]
Produces: 1 year, 1 month, - 1 month out Code Select
[event=2019/03/11][/event]
Produces: 1 year, 1 month, 1 day - 1 month outAs you can see some dates work perfectly some don't - any help would be gratefully received