PHP函數gmstrftime()將秒數轉換成天時分秒

來源:互聯網
上載者:User

標籤:fine   需要   title   str   turn   else   bug   1年   for   

http://yangjunwei.com/a/930.html

 

PHP函數gmstrftime()將秒數轉換成天時分秒 

一個應用情境需要用到倒計時的時分秒,比如新浪微博授權有效期間剩餘: 7天16小時47分鐘42秒……

在PHP環境下,PHP函數 gmstrftime() 可實現將秒數轉換成時分秒的轉換,先看例子:

define("BJTIMESTAMP" , time()); //伺服器目前時間$expires_in= ‘1439577160‘;//到期時間$expires= $expires_in - BJTIMESTAMP;function time2second($seconds){$seconds = (int)$seconds;if( $seconds<86400 ){//如果不到一天$format_time = gmstrftime(‘%H時%M分%S秒‘, $seconds);}else{$time = explode(‘ ‘, gmstrftime(‘%j %H %M %S‘, $seconds));//Array ( [0] => 04 [1] => 14 [2] => 14 [3] => 35 ) $format_time = ($time[0]-1).‘天‘.$time[1].‘時‘.$time[2].‘分‘.$time[3].‘秒‘;}return $format_time;}echo "新浪微博授權有效期間剩餘: ". time2second($expires) . ‘<hr>‘;

註:gmstrftime() 函數返回的天數是一年中的第幾天,因此時間超過一年,請使用下述代碼。

更細緻的劃分,可用以下例子:

function time2second($seconds){$seconds = (int)$seconds;if( $seconds>3600 ){if( $seconds>24*3600 ){$days= (int)($seconds/86400);$days_num= $days."天";$seconds= $seconds%86400;//取餘}$hours = intval($seconds/3600);$minutes = $seconds%3600;//取餘下秒數$time = $days_num.$hours."小時".gmstrftime(‘%M分鐘%S秒‘, $minutes);}else{$time = gmstrftime(‘%H小時%M分鐘%S秒‘, $seconds);}return $time;}echo "新浪微博授權有效期間剩餘: ". time2second($expires) . ‘<hr>‘;

最後說一下函數 gmstrftime() 的用法及參數:

文法:

gmstrftime(format,timestamp)

參數及描述:

format必要參數;指定了返回結果的方法:%a - abbreviated weekday name 縮減的表示星期幾的名稱%A - full weekday name 表示星期幾的全稱%b - abbreviated month name 月份簡稱%B - full month name 月份全稱%c - preferred date and time representation 首選的日期和時間標記法%C - century number (the year divided by 100, range 00 to 99) 表示世紀的數字(年份除以100,範圍從00到99)%d - day of the month (01 to 31) 一個月包含的天數(從01到31)%D - same as %m/%d/%y 時間格式,與%m/%d/%y標記法相同%e - day of the month (1 to 31) 一個月包含的天數,數字前不包括0(從1到31)%g - like %G, but without the century 與%G雷同,但除去“世紀[century]”%G - 4-digit year corresponding to the ISO week number (see %V). 與ISO星期數相對應的4位元年份(見%V)%h - same as %b 與%b相同%H - hour, using a 24-hour clock (00 to 23) 小時,使用24小時時鐘(00到23)%I - hour, using a 12-hour clock (01 to 12) 小時,使用12小時時鐘(01到12)%j - day of the year (001 to 366) 一年的天數(001到366)%m - month (01 to 12) 月份(01到12)%M - minute 分鐘%n - newline character 分行符號%p - either am or pm according to the given time value 與給定的時間值相對應的am或pm%r - time in a.m. and p.m. notation 用am或pm表示給定的時間%R - time in 24 hour notation 用24小時製表示的時間%S - second 秒%t - tab character tab鍵/定位字元%T - current time, equal to %H:%M:%S 目前時間,與“%H:%M:%S”組合相同%u - weekday as a number (1 to 7), Monday=1. Warning: In Sun Solaris Sunday=1 以數字形式表示星期幾(1到7),Monday=1。提醒:在SUN Sloaris系統中,Sunday=1%U - week number of the current year, starting with the first Sunday as the first day of the first week 當今年份中包含的周的總數,以第一個星期日作為第一周的第一天%V - The ISO 8601 week number of the current year (01 to 53), where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week 在當今年份中所包含的ISO 8601格式下的周的總數(01到53),week 1表示第一周,以周一作為每周的第一天%W - week number of the current year, starting with the first Monday as the first day of the first week 當前年份中包含的周的總數,以第一個星期一作為第一周的第一天%w - day of the week as a decimal, Sunday=0 以數位形式表示星期幾,Sunday[星期日]=0%x - preferred date representation without the time 選取除去時間[time]的日期[date]%X - preferred time representation without the date 選取除去日期[date]的時間[time]%y - year without a century (range 00 to 99) 只顯示包含年份的數字,不包含表示世紀的數字(00-99)%Y - year including the century 顯示包含世紀數位年份(即:四位元字表示的年份,如:1999,2001等)%Z or %z - time zone or name or abbreviation 前者為時區名稱;後者為時區名稱的簡稱%% - a literal % character 輸出“%”字串timestamp選擇性參數。指定日期或時間的格式。如果沒有指定時間戳記[timestamp],那麼將預設使用當前的GMT時間。

提示:gmstrftime()函數與strftime()函數用法大致相同,唯一的不同點是gmstrftime()函數返回的格林威治時間(GMT:Greenwich Mean Time)

  4 條評論 »
  • 開到荼蘼 
    2016/09/21 at 15:21:24Log in to Reply

    有個bug,時間超過1年結果就不對了。。。

    • YangJunwei 
      2016/10/13 at 17:15:07Log in to Reply

      @開到荼蘼 gmstrftime()返回一年中的第幾天,超過一年請使用第二個函數。

      •  
  

PHP函數gmstrftime()將秒數轉換成天時分秒

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.