PHPbb3使用BBCODE日期的Javascript内联倒计时器不太正确

时间:2018-03-11 18:41:16

标签: javascript date phpbb3

如果这是在错误的区域,请注意!

我试图在帖子事件倒数计时器中创建'内联',理想情况下使用BBcode,以便论坛用户可以为某些事件创建倒数计时器。

到目前为止,我所做的一切都很有效,但有些数学并不完全正确,所以虽然大多数日期都能正确显示,但有些事情却没有,而且我最终试图解决我出错的地方!有人可以看看并指出我正确的方向 - 要清楚 - 我使用的代码不是我自己的。

我在PHPBB 3.2.0上

BBCode用法

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

HTML替换

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

帮助热线

[event=DATE]NAME THE EVENT[/event] Example : [event=2008/09/22]Next workday[/event]

今天在我的论坛上测试这个(今天的日期是:2018/03/10:YYYY / MM / DD)我得到以下结果(香港专业教育学院展示了每个活动代码前面的内容)

[event=2018/03/09][/event]制作:昨天 - 正确

[event=2018/03/10][/event]制作:今天 - 正确

[event=2018/03/11][/event]制作:明天 - 正确

[event=2019/03/09][/event]制作:1年26天 - 错误

[event=2019/03/10][/event]产生:1年,1个月,1个月

[event=2019/03/11][/event]产生:1年,1个月,1天 - 1个月

正如你所看到的,某些日期可以完美地发挥作用 - 有任何帮助都会感激不尽

0 个答案:

没有答案