PHP時間戳記與日期之間轉換的執行個體介紹_php執行個體

來源:互聯網
上載者:User

1.php中時間轉換函式

strtotime(“today”)

date("Y-m-d H:i",$unixtime)

2.php中獲得今天零點的時間戳記

要獲得零點的unix時間戳記,可以使用$todaytime=strtotime(“today”),

然後再使用date("Y-m-d H:i",$todaytime)轉換為日期。

3.php中時間戳記轉換為日期,並按照時間顯示不同的內容,如剛剛,分鐘前,小時前,今天,昨天等

/*時間轉換函式*/

function transTime($ustime) {           

  $ytime = date("Y-m-d H:i",$ustime);             

  $rtime = date("n月j日 H:i",$ustime);           

  $htime = date("H:i",$ustime);           

  $time = time() - $ustime;           

  $todaytime = strtotime("today");           

  $time1 = time() - $todaytime;                           

  if($time < 60){                   

    $str = '剛剛';           

  }else if($time < 60 * 60){                            

    $min = floor($time/60);                   

    $str = $min.'分鐘前';           

  }else if($time < $time1){                   

    $str = '今天 '.$htime;           

  }else{                   

    $str = $rtime;           

  }             

  return $str;

}

在這個函數中你可以添加更多的比較使顯示的日期更加具體,比如添加 秒鐘前,前天等更具體的日期。

4.php日期中補0與不補0

echo date('Y-m-d'); 顯示2012-08-08

echo date('Y-n-j'); 顯示2012-8-8

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.