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:24

    有个bug,时间超过1年结果就不对了。。。

 
 

PHP函数gmstrftime()将秒数转换成天时分秒的更多相关文章

  1. PHP 将秒数转换成时分秒

    将秒数转换成时分秒,PHP提供了一个函数gmstrftime,不过该函数仅限于24小时内的秒数转换.对于超过24小时的秒数,我们应该怎么让其显示出来呢,例如 34:02:02 $seconds = 3 ...

  2. c# 将秒数转换成时,分,秒的方法

    TimeSpan ts = , ,Convert.ToInt32( duration)); string str = ""; ) { str = ts.Hours.ToString ...

  3. php 把秒数转换为时长(h:i:s格式)

    /** * 把秒数转换为时分秒的格式 * @param Int $times 时间,单位 秒 * @return String */ function secToTime($times){ $resu ...

  4. iOS 秒数转换成时间,时,分,秒

    //转换成时分秒 - (NSString *)timeFormatted:(int)totalSeconds{ int seconds = totalSeconds % 60;     int min ...

  5. 微信小程序中利用时间选择器和js无计算实现定时器(将字符串或秒数转换成倒计时)

    转载注明出处 改成了一个单独的js文件,并修改代码增加了通用性,点击这里查看 今天写小程序,有一个需求就是用户选择时间,然后我这边就要开始倒计时. 因为小程序的限制,所以直接选用时间选择器作为选择定时 ...

  6. js秒数转换时分秒方法

    今天写一个东西的时候 发现给出的是秒数.实在找不到直接的工具去转换. 就去网上找了个转换方法(有现成的就不写了,以后再简化下代码). function formatSeconds(value) { v ...

  7. date 命令之日期和秒数转换

    时间转为秒数 date -d "2012-11-12 13:00:00" +"%s" 描述转为日期 date -d@1352692800 +"%Y-% ...

  8. java将秒数转换为时分秒格式

    /** * 转换时间格式为xx小时xx分xx秒 * @param second xxxxx */ public String changeTimeFormat(String second) { Int ...

  9. PHP——秒数转换为时分秒

    前言 通讯记录需要用到的一个方法,就是将秒转为时分秒 方法 PHP有内置的方法,直接用即可,不过这个只是24小时以内.对于通讯录来说是够用了~ 示例 $v = 30; gmdate('H:i:s', ...

随机推荐

  1. Django1.0和2.0中的rest_framework的序列化组件之超链接字段的处理

    大家看到这个标题是不是有点懵逼,其实我就是想要一个这样的效果 比如我get一条书籍的数据,在一对多的字段中我们显示一个url,看起来是不是很绚! 下面我们就来实现这么一个东西 首先我们一对多字段中的一 ...

  2. unittest 单元测试

    unittest 单元测试: 1,单元测试是指对软件中最小可测试单元进行检查和验证.对于单元测试中单元的含义,一般来讲,要根据实际情况去判定其具体含义. 2,unitest=TestCase + Te ...

  3. 联想G510安装win7系统

    1.插入系统盘重启(要按Fn+f12) 2默认下一步 3.选择自定义 4.选择需要将系统安装到的分区,格式化,然后下一步 5.默认下一步 安装到这里就成功了 6.问题处理: 第4步时出现 Window ...

  4. 通配符的匹配很全面, 但无法找到元素 'tx:annotation-driven' 的声明

    启动Tomcat时报错,通配符的匹配很全面, 但无法找到元素 'tx:annotation-driven' 的声明,报错如下 1.从报错可以看到找不到元素   tx:annotation-driven ...

  5. 成为JAVA架构师必看书籍推荐

    原创文章 “学习的最好途径就是看书“,这是我自己学习并且小有了一定的积累之后的第一体会.个人认为看书有两点好处: 1.能出版出来的书一定是经过反复的思考.雕琢和审核的,因此从专业性的角度来说,一本好书 ...

  6. win下Apache2.4的下载与安装

    1.到apache官网上下载apache的安装文件 http://httpd.apache.org/download.cgi   点击链接Files for Microsoft Windows,因为a ...

  7. Aspose.words一 DOM结构

    2.文档对象模型概述 2.1 DOM介绍 Aspose.Words的文档对象模型(以下简称DOM)是一个Word文档在内存中的映射,Aspose.Words的DOM可以编程读取.操作和修改Word文档 ...

  8. springboot报错Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean

    springboot项目在启动时需要把servlet容器编译进去,这时候如果你的maven依赖里面没有配置jetty或者tomcat相关依赖就会报错. 解决方法: jetty添加 <depend ...

  9. 访问WebServcie遇到配额不足的时候,请增加配额

    常常遇到的报错: 1.错误一: Error in deserializing body of reply message for operation 'GetArticleInfo'.,StackTr ...

  10. sql:inner join,left join,right join,full join用法及区别

    join的语法: select [字段] from [表名1] inner/left/right/full join [表名2] on [表名1.字段1] <关系运算符> [表名2.字段2 ...