define('DATEFORMAT', 'Y-m-d'); // 日期格式 define('TIMEFORMAT', 'H:i:s'); //时间格式 define('DATECONVERT', 1); //是否使用人性化时间格式 define('TIMEOFFSET', '8'); //默认所在时区 define('TIMESTAMP', time()); $lang = array( 'before' => '前', 'day' => '天', 'yday' => '昨天', 'byday' => '前天', 'hour' => '小时', 'half' => '半', 'min' => '分钟', 'sec' => '秒', 'now' => '刚刚', ); $timestamp = strtotime('2014-3-12 10:20:34'); $time = dgmdate($timestamp, 'u'); echo $time; function dgmdate($timestamp, $format = 'dt', $timeoffset = '9999', $uformat = '') { global $lang; $format == 'u' && !DATECONVERT && $format = 'dt'; static $dformat, $tformat, $dtformat, $offset; if ($dformat === null) { $dformat = DATEFORMAT; $tformat = TIMEFORMAT; $dtformat = $dformat . ' ' . $tformat; $offset = TIMEOFFSET; } $timeoffset = $timeoffset == 9999 ? $offset : $timeoffset; $timestamp += $timeoffset * 3600; $format = empty($format) || $format == 'dt' ? $dtformat : ($format == 'd' ? $dformat : ($format == 't' ? $tformat : $format)); if ($format == 'u') { $todaytimestamp = TIMESTAMP - (TIMESTAMP + $timeoffset * 3600) % 86400 + $timeoffset * 3600; $s = gmdate(!$uformat ? str_replace(":i", ":i:s", $dtformat) : $uformat, $timestamp); $time = TIMESTAMP + $timeoffset * 3600 - $timestamp; if ($timestamp >= $todaytimestamp) { if ($time > 3600) { return '<span title="' . $s . '">' . intval($time / 3600) . ' ' . $lang['hour'] . $lang['before'] . '</span>'; } elseif ($time > 1800) { return '<span title="' . $s . '">' . $lang['half'] . $lang['hour'] . $lang['before'] . '</span>'; } elseif ($time > 60) { return '<span title="' . $s . '">' . intval($time / 60) . ' ' . $lang['min'] . $lang['before'] . '</span>'; } elseif ($time > 0) { return '<span title="' . $s . '">' . $time . ' ' . $lang['sec'] . $lang['before'] . '</span>'; } elseif ($time == 0) { return '<span title="' . $s . '">' . $lang['now'] . '</span>'; } else { return $s; } } elseif (($days = intval(($todaytimestamp - $timestamp) / 86400)) >= 0 && $days < 7) { if ($days == 0) { return '<span title="' . $s . '">' . $lang['yday'] . ' ' . gmdate($tformat, $timestamp) . '</span>'; } elseif ($days == 1) { return '<span title="' . $s . '">' . $lang['byday'] . ' ' . gmdate($tformat, $timestamp) . '</span>'; } else { return '<span title="' . $s . '">' . ($days + 1) . ' ' . $lang['day'] . $lang['before'] . '</span>'; } } else { return $s; } } else { return gmdate($format, $timestamp); } }