这是一个JS Fiddle。剧本不言自明。我只想指出它不起作用。请看一下并告诉我该怎么做。提前致谢。我遵循了我认为是 javascript 编程中的每条规则,但不知何故,我一定忽略了一些东西。我还制作了一个实际的工作版本,即 PHP 脚本。工作中的 PHP 是本文中的第二个脚本:PHP split string at last number, insert a extra string and merge the new string。
function calTime(x) {
if (x === '') {
x = 54098;
} // Time in seconds
var f = 31536000, // seconds in a year
d = 86400, // seconds in a day
h = 3600, // seconds in an hour
m = 60, // seconds in a minute
xa,
xb,
xc,
xe,
xq,
string,
lb_y = 'year',
lb_ys = 'years',
lb_d = 'day',
lb_ds = 'days',
lb_h = 'hour',
lb_hs = 'hours',
lb_m = 'minute',
lb_ms = 'minutes',
lb_s = 'second',
lb_ss = 'seconds',
lb_and = 'and';
// a = years
var a = x / f;
// To prevent complications using scientific numbers less than 0 ex 7.2341232E-23
var a1 = a.indexOf("E-");
if (a1) {
a = 0;
}
// Split a so we only get the numbers before '.'
var a2 = a.indexOf(".");
if (a2) {
Math.floor(a);
}
// if $a is less or equal to 0 - it is 0
if (a <= 0) {
a = 0;
}
// b = days
var b = (x - (f * a)) / d;
// To prevent complications using scientific numbers less than 0 ex 7.2341232E-23
var b1 = b.indexOf("E-");
if (b1) {
b = 0;
}
// Split b so we only get the numbers before '.'
var b2 = b.indexOf(".");
if (b2) {
Math.floor(b);
}
// if $b is less or equal to 0 - it is 0
if (b <= 0) {
b = 0;
}
// c = hours
var c = (x - (f * a) - (d * b)) / h;
// To prevent complications using scientific numbers less than 0 ex 7.2341232E-23
var c1 = c.indexOf("E-");
if (c1) {
c = 0;
}
// Split c so we only get the numbers before '.'
var c2 = c.indexOf(".");
if (c2) {
Math.floor(c);
}
// if $c is less or equal to 0 - it is 0
if (c <= 0) {
c = 0;
}
// e = minutes
var e = (x - (f * a) - (d * b) - (h * c)) / m;
// Split $e so we only get the numbers before '.'
var e2 = e.indexOf(".");
if (e2) {
Math.floor(e);
}
// if $e is less or equal to 0 - it is 0
if (e <= 0) {
e = 0;
}
// $q = seconds
var q = (x - (f * a) - (d * b) - (h * c) - (m * e));
// Rewrite numbers if below 9
if (a <= 9) {
xa = '0' + a;
} else {
xa = a;
}
if (b <= 9) {
xb = '0' + b;
} else {
xb = b;
}
if (c <= 9) {
xc = '0' + c;
} else {
xc = c;
}
if (e <= 9) {
xe = '0' + e;
} else {
xe = e;
}
if (q <= 9) {
xq = '0' + q;
} else {
xq = q;
}
// Rewrite labels
if (a <= 1) {
lb_ys = lb_y;
}
if (b <= 1) {
lb_ds = lb_d;
}
if (c <= 1) {
lb_hs = lb_h;
}
if (e <= 1) {
lb_ms = lb_m;
}
if (q <= 1) {
lb_ss = lb_s;
}
// if == 0 - do not show
if (a === 0) {
a = '';
} else {
a = a + ' ' + lb_ys;
}
if (b === 0) {
b = '';
} else {
b = b + ' ' + lb_ds;
}
if (c === 0) {
c = '';
} else {
c = c + ' ' + lb_hs;
}
if (e === 0) {
e = '';
} else {
e = e + ' ' + lb_ms;
}
if (q === 0) {
q = '';
} else {
q = q + ' ' + lb_ss;
}
var time = [a, b, c, e, q];
time = time.filter(Number);
var count = time.count();
var last = time[time.length - 1];
if (count == 1) {
string = last;
} else if (count === 0) {
string = '<i>No Time described</i>';
} else {
string = time.join(', ') + ' ' + lb_and + ' ' + last;
}
return string;
}
document.getElementById("demo").innerHTML = calTime(83200);