PHPBB bbcode help - event countdown timer

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Post Reply
x2sln
Registered User
Posts: 1
Joined: Sat Mar 10, 2018 2:19 pm

PHPBB bbcode help - event countdown timer

Post by x2sln »

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

Code: Select all

[event={NUMBER1}/{NUMBER2}/{NUMBER3}]{TEXT}[/event]
HTML Replacement

Code: Select all

<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 all

[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 all

[event=2018/03/09][/event] 
Produces: Yesterday - correct

Code: Select all

[event=2018/03/10][/event] 
Produces: Today - correct

Code: Select all

[event=2018/03/11][/event] 
Produces: Tomorrow - correct

Code: Select all

[event=2019/03/09][/event] 
Produces: 1 year, 26 days - wrong

Code: Select all

[event=2019/03/10][/event] 
Produces: 1 year, 1 month, - 1 month out

Code: Select all

[event=2019/03/11][/event] 
Produces: 1 year, 1 month, 1 day - 1 month out

As you can see some dates work perfectly some don't - any help would be gratefully received
Last edited by x2sln on Sun Mar 11, 2018 7:52 pm, edited 1 time in total.
User avatar
stevemaury
Support Team Member
Support Team Member
Posts: 51905
Joined: Thu Nov 02, 2006 12:21 am
Location: The U.P.
Name: Steve
Contact:

Re: PHPBB bbcode help - event countdown timer

Post by stevemaury »

Welcome to phpBB! Your question is not really either a phpBB support question, nor is it a BBCode issue, per se. I will move this to the custom coding forum and we will se if someone there can help.

However, in the meantime, I think you should try to ask this wherever it is you got this code.
For REALLY good and VERY inexpensive hosting CLICK HERE

I can stop all your spam. I can upgrade or update your Board. PM or email me. (Paid support)
User avatar
canonknipser
Registered User
Posts: 2096
Joined: Thu Sep 08, 2011 4:16 am
Location: Germany
Name: Frank Jakobs
Contact:

Re: PHPBB bbcode help - event countdown timer

Post by canonknipser »

I would check the daysInFebruary function first or try other ways to achieve the date difference, just google for "javascript date calculate difference between dates" eg: https://www.htmlgoodies.com/html5/javas ... cript.html
Greetings, Frank
phpbb.de support team member
English is not my native language - no support via PM or mail
New arrival - Extensions and scripts for phpBB
User avatar
mrgoldy
Development Team Member
Development Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: PHPBB bbcode help - event countdown timer

Post by mrgoldy »

There is an actual php class for this: DateInterval. Have a look at it. It will do all the calculations for you, including how many days there are in februari.
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
canonknipser
Registered User
Posts: 2096
Joined: Thu Sep 08, 2011 4:16 am
Location: Germany
Name: Frank Jakobs
Contact:

Re: PHPBB bbcode help - event countdown timer

Post by canonknipser »

posey wrote:
Sat Mar 10, 2018 9:41 pm
There is an actual php class for this:
I don't know if you can call php scripts in a custom bbCode ...
Greetings, Frank
phpbb.de support team member
English is not my native language - no support via PM or mail
New arrival - Extensions and scripts for phpBB
User avatar
stevemaury
Support Team Member
Support Team Member
Posts: 51905
Joined: Thu Nov 02, 2006 12:21 am
Location: The U.P.
Name: Steve
Contact:

Re: PHPBB bbcode help - event countdown timer

Post by stevemaury »

And remember, 2400 will NOT be a leap year.
For REALLY good and VERY inexpensive hosting CLICK HERE

I can stop all your spam. I can upgrade or update your Board. PM or email me. (Paid support)
User avatar
mrgoldy
Development Team Member
Development Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: PHPBB bbcode help - event countdown timer

Post by mrgoldy »

canonknipser wrote:
Sat Mar 10, 2018 9:52 pm
posey wrote:
Sat Mar 10, 2018 9:41 pm
There is an actual php class for this:
I don't know if you can call php scripts in a custom bbCode ...
Yeah you're right, blanked out here. :flushed:
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
AmigoJack
Registered User
Posts: 5841
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン
Contact:

Re: PHPBB bbcode help - event countdown timer

Post by AmigoJack »

x2sln wrote:
Sat Mar 10, 2018 2:22 pm

Code: Select all

language="JavaScript"
document.write
From whichever grave you exhumed this script - put it back and let it rest. It has several issues and it's rather frightening it is actually executed by any internet browser. Oh, and it will most probably collide with itself when being used more than once on the same page.

x2sln wrote:
Sat Mar 10, 2018 2:22 pm

Code: Select all

[event={NUMBER1}/{NUMBER2}/{NUMBER3}]{TEXT}[/event]

Code: Select all

Date('{NUMBER1}/{NUMBER2}/{NUMBER3}')
Why would you define a BBCode which needs three separate parameters when you pass them in exactly that order again? Not to mention it's a date format mostly never recognized by computers?

x2sln wrote:
Sat Mar 10, 2018 2:22 pm

Code: Select all

[event=2019/03/09][/event] 
Produces: 1 year, 26 days - wrong
This works as intended, since yyyy/mm/dd is no recognized date format. How should JavaScript know which component is the month and which is the day? Stick to ISO 8601 (yyyy-mm-dd) or RFC2822 (d MMM, yyyy) - any other format is subject to each internet browser's own parsing support and thus must be considered to fail. Alternatively use Date() with parameters for each date component (where the months starts with 0).

Trivia: https://en.wikipedia.org/wiki/Date_format_by_country
  • "The problem is probably not my English but you do not want to understand correctly. ... We will not come anybody anyway, nevertheless, it's best to shit this." Affin, 2018-11-20
  • "But this shit is not here for you. You can follow with your. Maybe the question, instead, was for you, who know, so you shoved us how you are." axe70, 2020-10-10
  • "My reaction is not to everyone, especially to you." Raptiye, 2021-02-28
Post Reply

Return to “phpBB Custom Coding”