由于我之前的回答让人们完全忽略了这一点,这里有一个 PHP 代码端口,我必须做同样的事情:
function getDaysInMonth(month,year) {
if( typeof year == "undefined") year = 1999; // any non-leap-year works as default
var currmon = new Date(year,month),
nextmon = new Date(year,month+1);
return Math.floor((nextmon.getTime()-currmon.getTime())/(24*3600*1000));
}
function getDateTimeSince(target) { // target should be a Date object
var now = new Date(), diff, yd, md, dd, hd, nd, sd, out = [];
diff = Math.floor(now.getTime()-target.getTime()/1000);
yd = target.getFullYear()-now.getFullYear();
md = target.getMonth()-now.getMonth();
dd = target.getDate()-now.getDate();
hd = target.getHours()-now.getHours();
nd = target.getMinutes()-now.getMinutes();
sd = target.getSeconds()-now.getSeconds();
if( md < 0) {yd--; md += 12;}
if( dd < 0) {
md--;
dd += getDaysInMonth(now.getMonth()-1,now.getFullYear());
}
if( hd < 0) {dd--; hd += 24;}
if( md < 0) {hd--; md += 60;}
if( sd < 0) {md--; sd += 60;}
if( yd > 0) out.push( yd+" year"+(yd == 1 ? "" : "s"));
if( md > 0) out.push( md+" month"+(md == 1 ? "" : "s"));
if( dd > 0) out.push( dd+" day"+(dd == 1 ? "" : "s"));
if( hd > 0) out.push( hd+" hour"+(hd == 1 ? "" : "s"));
if( nd > 0) out.push( nd+" minute"+(nd == 1 ? "" : "s"));
if( sd > 0) out.push( sd+" second"+(sd == 1 ? "" : "s"));
return out.join(" ");
}
例子:
getDateTimeSince(new Date(1992,1,6,22,30,00));
// my date of birth - near enough half past ten in the evening on Feb 6th 1992
> 20 years 1 month 18 days 17 hours 23 minutes 7 seconds
我相信这正是 OP 所要求的。