時間は今からどのくらいの形式で表して、PHP、js双バージョン
4889 ワード
くだらないことを言わないで直接コードをつけなさい.
PHP FOR Smarty
Javascript
JSは複雑な点があり、3つの関数に分けて実現する
PHP FOR Smarty
* Name: time_ago
* Purpose:
* 1
* 1
* 1
* 3 ,
* 3
*
* @author Peter Pan
* @param int $time input int
*/
function smarty_modifier_time_ago($time) {
$time_deff = time() - $time;
$retrun = '';
if ($time_deff >= 259200) {
$retrun = date('Y-m-d H:i', $time);
} else if ($time_deff >= 172800) {
$retrun = " " . date('H:i', $time);
} else if ($time_deff >= 86400) {
$retrun = " " . date('H:i', $time);
} else if ($time_deff >= 3600) {
$hour = intval($time_deff / 3600);
$minute = intval(($time_deff % 3600) / 60);
$retrun = $hour . ' ';
if ($minute > 0) {
$retrun .= $minute . ' ';
}
$retrun .= ' ';
} else if ($time_deff >= 60) {
$minute = intval($time_deff / 60);
$second = $time_deff % 60;
$retrun = $minute . ' ';
if ($second > 0) {
$retrun .= $second . ' ';
}
$retrun .= ' ';
}else{
$retrun = $time_deff.' ';
}
return $retrun;
}
Javascript
JSは複雑な点があり、3つの関数に分けて実現する
/**
*
* @param string str
* @param int len
* @param str chr
* @param str dir left|right|both right
*/
function strPad(str, len, chr, dir){
str = str.toString();
len = (typeof len == 'number') ? len : 0;
chr = (typeof chr == 'string') ? chr : ' ';
dir = (/left|right|both/i).test(dir) ? dir : 'right';
var repeat = function(c, l) {
var repeat = '';
while (repeat.length < l) {
repeat += c;
}
return repeat.substr(0, l);
}
var diff = len - str.length;
if (diff > 0) {
switch (dir) {
case 'left':
str = '' + repeat(chr, diff) + str;
break;
case 'both':
var half = repeat(chr, Math.ceil(diff / 2));
str = (half + str + half).substr(1, len);
break;
default:
str = '' + str + repeat(chr, diff);
}
}
return str;
}
/**
*
* php Date , Unix ( )
* ( ):
* y 4
* m 2
* d 2
* h 2
* i 2
* s 2
*/
function formatDate(format,timestamp) {
var date = new Date(parseInt(timestamp) * 1000);
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
month = strPad(month,2,'0','left');
day = strPad(day,2,'0','left');
hour = strPad(hour,2,'0','left');
minute = strPad(minute,2,'0','left');
second = strPad(second,2,'0','left');
format = format.replace(/y/gi,year);
format = format.replace(/m/gi,month);
format = format.replace(/d/gi,day);
format = format.replace(/h/gi,hour);
format = format.replace(/i/gi,minute);
format = format.replace(/s/gi,second);
return format;
}
function timeAgo(time) {
var nowTime = Date.parse(new Date()) / 1000;
var time_deff = nowTime - time;
retrun = '';
if (time_deff >= 259200) {
retrun = formatDate('Y-m-d H:i', time);
} else if (time_deff >= 172800) {
retrun = " " + formatDate('H:i', time);
} else if (time_deff >= 86400) {
retrun = " " + formatDate('H:i', time);
} else if (time_deff >= 3600) {
hour = parseInt(time_deff / 3600);
minute = parseInt((time_deff % 3600) / 60);
retrun = hour + ' ';
if (minute > 0) {
retrun += minute + ' ';
}
retrun += ' ';
} else if (time_deff >= 60) {
minute = parseInt($time_deff / 60);
second = time_deff % 60;
retrun = minute + ' ';
if (second > 0) {
retrun += second + ' ';
}
retrun += ' ';
}else{
retrun = time_deff +' ';
}
return retrun;
}