最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP常用函數(shù)之格式化時間操作示例

 更新時間:2019年10月21日 11:17:36   作者:學(xué)知無涯  
這篇文章主要介紹了PHP常用函數(shù)之格式化時間操作,結(jié)合實例形式分析了php針對日期時間的計算、轉(zhuǎn)換及格式化等相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了PHP常用函數(shù)之格式化時間操作。分享給大家供大家參考,具體如下:

/**
 * 格式化時間
 * @param $time 時間戳
 * @return bool|string
 */
function formatTimeToNow($time){
  //獲取現(xiàn)在的時間戳
  $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) . "小時前";//如果大于1小時
    }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));
}

/**
 * 計算自然周期的開始時間戳和結(jié)束時間戳(周一到周日,月初到月末)
 * @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點的時間戳
  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;
  }
}

/**
 * 計算近一周或近一個月的開始時間戳和結(jié)束時間戳
 * @param $type 1表示今天,2表示近一周,3表示近一個月
 * @return array
 */
function nearFormatTime($type){
  $start_time = strtotime(date('Y-m-d 00:00:00'));//今天0點的時間戳
  $end_time = $start_time + 86399;//今天23:59的時間戳
  $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){
    //近一個月
    $res['start_time'] = $start_time - 86400*30;//包括今天,共31天
  }
  return $res;
}

PS:這里再為大家推薦幾款時間及日期相關(guān)工具供大家參考:

在線日期/天數(shù)計算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi

在線日期計算器/相差天數(shù)計算器:
http://tools.jb51.net/jisuanqi/datecalc

在線日期天數(shù)差計算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq

Unix時間戳(timestamp)轉(zhuǎn)換工具:
http://tools.jb51.net/code/unixtime

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php日期與時間用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論

商水县| 黄陵县| 大英县| 新沂市| 延川县| 平塘县| 临城县| 邯郸市| 文山县| 日照市| 苏尼特右旗| 大埔县| 双桥区| 桃源县| 东海县| 历史| 精河县| 遂宁市| 三明市| 常熟市| 伊吾县| 海阳市| 迁西县| 江油市| 秀山| 东至县| 台北县| 铜山县| 叶城县| 龙口市| 三亚市| 和平区| 都昌县| 沅江市| 江安县| 通道| 怀宁县| 遵义县| 苍溪县| 普格县| 若尔盖县|