本文實(shí)例講述了PHP常用函數(shù)之格式化時(shí)間操作。分享給大家供大家參考,具體如下:
/**
* 格式化時(shí)間
* @param $time 時(shí)間戳
* @return bool|string
*/
function formatTimeToNow($time){
//獲取現(xiàn)在的時(shí)間戳
$nowtime = time();
if($time>$nowtime){
return '';
}else{
$tc = $nowtime-$time;
if( $tc >= 864000 ){
$str = date('Y-m-d H:i',$time);//如果大于10天,則直接顯示日期
}else if( $tc >= 86400){
$str = floor($tc/86400) . "天前";//如果大于1天
}else if( $tc >= 3600) {
$str = floor($tc / 3600) . "小時(shí)前";//如果大于1小時(shí)
}else if( $tc >= 60) {
$str = floor($tc / 60) . "分鐘前";//如果大于1分鐘
}else{
$str = "剛剛";
}
return $str;
}
}
/**
* 將中文的日期格式化為正常的日期
* @param $date
* @return mixed
*/
function formatCnDateToDate($date){
//把年月替換為-,日替換為空
$date = str_replace('年','-',$date);
$date = str_replace('月','-',$date);
$date = str_replace('日','',$date);
//避免提交的格式不統(tǒng)一,例如2018-3-2等,標(biāo)準(zhǔn)化
return date('Y-m-d',strtotime($date));
}
/**
* 計(jì)算自然周期的開(kāi)始時(shí)間戳和結(jié)束時(shí)間戳(周一到周日,月初到月末)
* @param int $time_type 1表示自然天,2表示自然周,3表示自然月
* @param int $prev_num 距離現(xiàn)在的值(前一周傳-1,前兩周傳-2...)
* @return array|bool
*/
function naturalFormatTime($time_type=1,$prev_num=0){
$today_start_time = strtotime(date('Y-m-d 00:00:00',time()));//今天0點(diǎn)的時(shí)間戳
if($time_type == 1){
if($prev_num == 0){
return array('start_time'=>$today_start_time,'end_time'=>time(),'show_date'=>date('Y年m月d日',time()));
}else if($prev_num 0){
$start_time = $today_start_time - 86400*abs($prev_num);
$end_time = $start_time + 86399;
$show_date = date('Y年m月d日',$start_time);
return array('start_time'=>$start_time,'end_time'=>$end_time,'show_date'=>$show_date);
}else{
return false;
}
}else if($time_type == 2){
$today_week = date('w',$today_start_time);
if($today_week == 0){
$today_week_start_time = $today_start_time - 86400*6;
}else{
$today_week_start_time = $today_start_time - 86400*($today_week-1);
}
if($prev_num == 0){
$show_date = date('Y年m月d日',$today_week_start_time);
$show_date .= '至' . date('d日',time());
return array('start_time'=>$today_week_start_time,'end_time'=>time(),'show_date'=>$show_date);
}else if($prev_num 0){
$start_time = $today_week_start_time - 86400*7 * abs($prev_num);
$end_time = $start_time + (86400*7-1);
$show_date = date('Y年m月d日',$start_time);
$show_date .= '至' . date('d日',$end_time);
return array('start_time'=>$start_time,'end_time'=>$end_time,'show_date'=>$show_date);
}else{
return false;
}
}else if($time_type == 3){
if($prev_num == 0){
$today_day = ltrim(date('d',$today_start_time),0);
$today_month_start_time = $today_start_time - 86400*($today_day-1);
$show_date = date('Y年m月d日',$today_month_start_time);
$show_date .= '至' . date('d日',time());
return array('start_time'=>$today_month_start_time,'end_time'=>time(),'show_date'=>$show_date);
}else if($prev_num 0){
$start_time = strtotime(date('Y-m-01', strtotime("$prev_num month")));
$days = date('t',$start_time);
$end_time = $start_time + 86400 * $days -1;
$show_date = date('Y年m月d日',$start_time);
$show_date .= '至' . date('d日',$end_time);
return array('start_time'=>$start_time,'end_time'=>$end_time,'show_date'=>$show_date);
}else{
return false;
}
}else{
return false;
}
}
/**
* 計(jì)算近一周或近一個(gè)月的開(kāi)始時(shí)間戳和結(jié)束時(shí)間戳
* @param $type 1表示今天,2表示近一周,3表示近一個(gè)月
* @return array
*/
function nearFormatTime($type){
$start_time = strtotime(date('Y-m-d 00:00:00'));//今天0點(diǎn)的時(shí)間戳
$end_time = $start_time + 86399;//今天23:59的時(shí)間戳
$res = array('start_time'=>0,'end_time'=>$end_time);
if($type == 1){
//今天
$res['start_time'] = $start_time;
}else if($type == 2){
//近一周
$res['start_time'] = $start_time - 86400*6;//包括今天,共七天
}else if($type == 3){
//近一個(gè)月
$res['start_time'] = $start_time - 86400*30;//包括今天,共31天
}
return $res;
}
PS:這里再為大家推薦幾款時(shí)間及日期相關(guān)工具供大家參考:
在線日期/天數(shù)計(jì)算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在線日期計(jì)算器/相差天數(shù)計(jì)算器:
http://tools.jb51.net/jisuanqi/datecalc
在線日期天數(shù)差計(jì)算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq
Unix時(shí)間戳(timestamp)轉(zhuǎn)換工具:
http://tools.jb51.net/code/unixtime
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php日期與時(shí)間用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語(yǔ)法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- php Smarty date_format [格式化時(shí)間日期]
- php格式化時(shí)間戳顯示友好的時(shí)間實(shí)現(xiàn)思路及代碼
- php自定義的格式化時(shí)間示例代碼
- php格式化時(shí)間戳
- PHP獲取當(dāng)前日期和時(shí)間及格式化方法參數(shù)
- PHP+Mysql日期時(shí)間如何轉(zhuǎn)換(UNIX時(shí)間戳和格式化日期)
- php格式化日期和時(shí)間格式化示例分享
- PHP格式化顯示時(shí)間date()函數(shù)代碼
- php時(shí)間戳格式化顯示友好的時(shí)間函數(shù)分享
- PHP日期函數(shù)date格式化UNIX時(shí)間的方法
- PHP使用gmdate實(shí)現(xiàn)將一個(gè)UNIX 時(shí)間格式化成GMT文本的方法