~~~
/**
* ------------------------------------------------------------------------------------
* 常用时间
* ------------------------------------------------------------------------------------
*/
date_default_timezone_set('Asia/Hong_Kong');
echo '30秒前:' . date('Y-m-d H:i:s', strtotime('-30 seconds')) . '<br />';
echo '30分钟前:' . date('Y-m-d H:i:s',strtotime('-30 minutes')) . '<br />';
echo '12小时前:' . date('Y-m-d H:i:s',strtotime('-12 hours')) . '<br />';
echo '一天前:' . date('Y-m-d H:i:s',strtotime('-1 day')) . '<br />';
echo '一周前:' . date('Y-m-d H:i:s',strtotime('-1 week')) . '<br />';
echo '一月前:' . date('Y-m-d H:i:s',strtotime('-1 months')) . '<br />';
echo '一年前:' . date("Y-m-d",strtotime("-1 year")) . '<br />';
echo '一周零两天四小时两秒后:' . date("Y-m-d G:H:s", strtotime("+1 week 2 days 4 hours 2 seconds")) . '<br />';
$firstday = date('Y-m-01', time()); //月初
$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));//月末
echo '月初:' . $firstday . '<br />';
echo '月末:' . $lastday . '<br />';
$begintime=date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d'),date('Y')));
$endtime=date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1);
echo '今天开始时间:' . $begintime . '<br />';
echo '今天结束时间:' . $endtime . '<br />';
/**
* ------------------------------------------------------------------------------------
* 计算多少秒/分/时/天/周/月/年之前
* ------------------------------------------------------------------------------------
* @param $ptime
* @return string
*/
function time_ago( $ptime ) {
$ptime = intval($ptime);
$etime = time() - $ptime;
if ($etime < 59) return '刚刚';
$interval = array (
12 * 30 * 24 * 60 * 60 => '年前 ('.date('Y-m-d', $ptime).')',
30 * 24 * 60 * 60 => '个月前 ('.date('Y-m-d', $ptime).')',
7 * 24 * 60 * 60 => '周前 ('.date('m-d', $ptime).')',
24 * 60 * 60 => '天前',
60 * 60 => '小时前',
60 => '分钟前',
1 => '秒前'
);
foreach ($interval as $secs => $str) {
$d = $etime / $secs;
if ($d >= 1) {
$r = round($d);
return $r . $str;
}
}
}
$time_ago = time_ago(time() - 60 * rand(1, 120));
echo $time_ago;
~~~