一个应用场景需要用到倒计时的时分秒,比如新浪微博授权有效期剩余: 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) . '


';

注: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) . '


';

最后说一下函数 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)

php 时分秒转时分_PHP函数gmstrftime()将秒数转换成天时分秒相关推荐

  1. PHP函数gmstrftime()将秒数转换成天时分秒

    一个应用场景需要用到倒计时的时分秒,比如新浪微博授权有效期剩余: 7天16小时47分钟42秒-- gmstrftime() 可实现将秒数转换成时分秒的转换,先看例子: <?phpdefine(& ...

  2. 将秒数转换成天具体的天时分秒

    /*** 将秒数转换成天具体的天时分秒* 比如172800S转换成2天0时0分0秒* @param second* @return*/ public String formatSecond(Objec ...

  3. C# 23. 秒数转成天时分秒显示

    int s = 2000; TimeSpan ts = new TimeSpan(0,0,s); label1Text += "时间:" + (int)ts.TotalDays + ...

  4. Hive秒数转成时分秒

    Hive秒数转成时分秒 问题:开发当中遇到秒数转成时分秒的情况 1.参考:在mysql中可以使用SEC_TO_TIME 2.套用到hive,没有SEC_TO_TIME函数 3.解决方案:使用多个函数进 ...

  5. 【Vue】vue如何将秒数转成“时分秒”格式

    vue如何将秒数转成"时分秒"格式 一.使用原生JS的Date库 二.使用day.js 三.拓展 今天给大家分享一下vue如何将秒数转成"时分秒"格式的相关知识 ...

  6. python时间转绝对秒数_python时间时分秒与秒数的互相转换

    受到Unix时间戳的启发,我发现时间转成秒数后会非常好处理,在程序当中不再是以字符串的形式处理,不管时间的加减还是获取随机的时间点都变得非常方便, 如果有需要,也很容易转换成需要的时间格式. 一:时间 ...

  7. java 秒转换日期_Java 将日期或秒数转换为日时分秒

    ```java import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; / ...

  8. js根据字符串时分秒获取总秒数和分割时分秒

    起初是因为一些网站的视频没找着时间戳,但是显示了视频的时长时分秒,为了做过滤处理,就想到了获取到视频上显示的的时分秒,进行截取,然后根据秒数判断该视频的时长进行筛选 /*** 分割时分秒字符串* @p ...

  9. 拿到倒计时秒数转成 时:分:秒格式

    倒计时功能的需要,从后台拿到服务器剩余的秒数,转化成 时:分:秒的格式展示到页面上,时分秒不足两位数时前面补0. /*** 秒数转 时:分:秒* @param {Number} time [秒数]*/ ...

最新文章

  1. 一顿关于心智、机器和智能的哲学大餐!!
  2. java使用Jsch实现远程操作linux服务器进行文件上传、下载,删除和显示目录信息...
  3. 皮一皮:有这样的妈妈挺有趣的...
  4. 一天搞定CSS:定位position--17
  5. ios不响应presentModalViewController界面的处理
  6. go语言基础到提高(7)-数组
  7. Xilinx:让FFmpeg在FPGA上玩的爽
  8. 华尔街顶级大师胡立阳名言
  9. vim怎么跳转到函数定义处_Vim、gvim操作跳转光标区块和代码块的跳转
  10. JavaScript学习(六十五)—数组知识点总结
  11. Leetcode之合并区间
  12. pdg转pdf与djvu转pdf大法
  13. linux检测摄像头驱动程序,linux usb 摄像头测试软件
  14. 《电子签名法》相关概念介绍
  15. 银行业务系统(c/s架构、socket网络编程、多线程)
  16. mysql 时区时间_MySql的时区(serverTimezone)引发的血案
  17. 我的母校照片~~``
  18. python自动获取微信公众号最新文章
  19. 无需下载软件pdf转jpg格式
  20. Word合并所有段落再按字数划分段落

热门文章

  1. 2012年中国兽药50强企业
  2. 安卓期末大作业——购物商城(源码+18页报告)
  3. 【yolact_edge】训练自己的yolact_edge模型(并部署在Jetson Xavier上)
  4. sublimetext3解决中文乱码问题
  5. 基于51单片机的智能浇花系统(可做毕设)
  6. PTA乙级 1069 微博转发抽奖——20分
  7. NotePad 打开文件 出现中文汉字乱码 解决办法
  8. [深度学习][原创]常用ocr框架和技术总结
  9. ALSA框架学习笔记3:声卡注册流程(代码解析)
  10. linux 从命令行启动,硬盘安装Linux和从Grub命令行启动操作系统