`
lianxiangbus
  • 浏览: 526244 次
文章分类
社区版块
存档分类
最新评论

将日期转换成时间戳 strtotime

 
阅读更多

另一篇有用的时间函数:http://hi.baidu.com/%BD%A3%BE%B2%B7%E3/blog/item/598fd1c6a59f630e0ef477d3.html(哥的百度空间,不过以后就用这个了)

strtotime能将任何英文文本的日期时间描述解析为Unix时间戳,我们结合mktime()或date()

格式化日期时间获取指定的时间戳,实现所需要的日期时间。

格式:int strtotime ( string $time [, int $now ] )

成功则返回时间戳,否则返回FALSE。在 PHP 5.1.0 之前本函数在失败时返回-1

<?php
$str
='NotGood';

//previoustoPHP5.1.0youwouldcomparewith-1,insteadoffalse
if(($timestamp=strtotime($str))===false){
echo
"Thestring($str)isbogus";
}else{
echo
"$str==".date('ldSofFYh:i:sA',$timestamp);
}
?>

  本函数预期接受一个包含美国英语日期格式的字符串并尝试将其解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数),其值相对于 now 参数给出的时间,如果没有提供此参数则用系统当前时间。

  本函数将使用 TZ 环境变量(如果有的话)来计算时间戳。自 PHP 5.1.0 起有更容易的方法来定义时区用于所有的日期/时间函数。此过程在 date_default_timezone_get() 函数页面中有说明。

Note : 如果给定的年份是两位数字的格式,则其值 0-69 表示 2000-2069,70-100 表示 1970-2000。

参数

time

被解析的字符串,格式根据 GNU ? 日期输入格式 的语法。在 PHP 5.0 之前,time 中不允许有毫秒数,自 PHP 5.0 起可以有但是会被忽略掉。

now

用来计算返回值的时间戳。 该参数默认值是当前时间time(),也可以设置为其他时间的时间戳(我一直忽略的一个功能啊,惭愧)

返回值: 成功则返回间戳,否则返回 FALSE 。在 PHP 5.1.0 之前本函数在失败时返回 -1,后面版本返回false.

  strtotime的第一个参数可以是我们常见的英文时间格式,比如“2008-8-20”或“10 September 2000 ”等等。也可以是以参数now为基准的时间描述,比如“+1 day”等等。

下面是后一种方式的可使用参数清单,其中“当前时间”是指strtotime第二个参数now的值,默认为当前时间

1.月,日英文名及其常用缩写清单:

january,february,march,april,may,june,july,august,september,sept,october,november,december,

sunday,monday,tuesday,tues,wednesday,wednes,thursday,thur,thurs,friday,saturday

2.时间参数和祥细描述:

am : the time is before noon 上午

pm : the time is noon or later 下午

year: one year; for example, “next year” 年,比如“next year”代表明年

month : one month; for example, “last month” 月,比如“last month”代表上一月

fortnight : two weeks; for example, “a fortnight ago” 两周,比如“a fortnight ago”代表两周前

week : one week 周

day: a day 天

hour: an hour 小时

minute : a minute 分钟

min : same as minute 同“minute”

second : a second 秒

sec : same as second 同“second”

3.相关和顺序说明:

+n/-n :以当前时间算,加个减指定的时间,比如”+1 hour”是指当前时间加一小时

ago :time relative to now; such as “24 hours ago”  以当前时间往前算,比如”24 hours ago”代表“24小时前”

tomorrow : 24 hours later than the current date and time 以当前时间(包括日期和时间)为标准,明天同一时间

yesterday : 24 hours earlier than the current date and time 以当前时间(包括日期和时间)为标准,昨天同一时间

today : the current date and time 当前时间(包括日期和时间)

now : the current date and time 当前时间(包括日期和时间)

last : modifier meaning “the preceding”; for example, “last tuesday” 代表“上一个”,比如“last tuesday”代表“上周二同一时间”

this : the given time during the current day or the next occurrence of the given time; for example, “this 7am” gives the timestamp for 07:00 on the current day, while “this week” gives the timestamp for one week from the current time 当天的指定时间或下面一个时间段的时间戳,比如“this 7am”给出当天7:00的时间戳,而“this week”给出的是从当前时间开始的一整周的时间戳,也就是当前时间(经本人测试:strtotime('this week')=strtotime('now'));

next : modifier meaning the current time value of the subject plus one; for example, “next hour” 当前时间加上指定的时间,比如“next hour”是指当前时间加上一小时,即加3600

实现功能:获取某个日期的时间戳,或获取某个时间的时间戳。

strtotime 将任何英文文本的日期时间描述解析为Unix时间戳[将系统时间转化成unix时间戳]

一,获取指定日期的unix时间戳 strtotime("2009-1-22") 示例如下:

echo strtotime("2009-1-22") 结果:1232553600

说明:返回2009年1月22日0点0分0秒时间戳

 二,获取英文文本日期时间 示例如下:

  便于比较,使用date将当时间戳与指定时间戳转换成系统时间

  (1)打印明天此时的时间戳strtotime("+1 day")

  当前时间:echo date("Y-m-d H:i:s",time()) 结果:2009-01-22 09:40:25

  指定时间:echo date("Y-m-d H:i:s",strtotime("+1 day")) 结果:2009-01-23 09:40:25

  (2)打印昨天此时的时间戳strtotime("-1 day")

  当前时间:echo date("Y-m-d H:i:s",time()) 结果:2009-01-22 09:40:25

  指定时间:echo date("Y-m-d H:i:s",strtotime("-1 day")) 结果:2009-01-2109:40:25

  (3)打印下个星期此时的时间戳strtotime("+1 week")

  当前时间:echo date("Y-m-d H:i:s",time()) 结果:2009-01-22 09:40:25

  指定时间:echo date("Y-m-d H:i:s",strtotime("+1 week")) 结果:2009-01-2909:40:25

(4)打印上个星期此时的时间戳strtotime("-1 week")

  当前时间:echo date("Y-m-d H:i:s",time()) 结果:2009-01-22 09:40:25

  指定时间:echo date("Y-m-d H:i:s",strtotime("-1 week")) 结果:2009-01-1509:40:25

  (5)打印指定下星期几的时间戳strtotime("next Thursday")

  当前时间:echo date("Y-m-d H:i:s",time()) 结果:2009-01-22 09:40:25

  指定时间:echo date("Y-m-d H:i:s",strtotime("next Thursday")) 结果:2009-01-29 00:00:00

  (6)打印指定上星期几的时间戳strtotime("last Thursday")

  当前时间:echo date("Y-m-d H:i:s",time()) 结果:2009-01-22 09:40:25

  指定时间:echo date("Y-m-d H:i:s",strtotime("last Thursday")) 结果:2009-01-15 00:00:00

  以上示例可知,strtotime能将任何英文文本的日期时间描述解析为Unix时间戳,我们

结合mktime()或date()格式化日期时间获取指定的时间戳,实现所需要的日期时间。

下面这个例子实现给一个时间输出加四个小时后的功能

<php?

$time="2011-4-20 20:20:20";

$timestamp=strtotime($time);//首先转换成时间戳

$t=date("Y-m_d H:i:s",$timestatmp+4*60*60);

?>

或者

<php?

$time="2011-4-20 20:20:20";

$timestamp=strtotime($time."+4 hour");//首先转换成时间戳

$t=date("Y-m_d H:i:s",$timestatmp);

?>

分享到:
评论

相关推荐

    PHP时间戳 strtotime()使用方法和技巧

    在php中我想要获取时间戳有多种方法,最常用的就是使用time函数与strtotime()函数把日期转换成时间戳了,下面我来给大家分享一下时间戳函数 strtotime用法。获取指定的年月日转化为时间戳:pHP时间戳函数获取指定...

    解析php时间戳与日期的转换

    大家也许对PHP时间戳已经有所了解...strtotime 将任何英文文本的日期时间描述解析为Unix时间戳[将系统时间转化成unix时间戳] 一,获取指定日期的unix时间戳 strtotime(”2009-1-22〃) 示例如下:echo strtotime(”2009-

    PHP中strtotime函数使用方法详解

    strtotime 实现功能:获取某个日期的时间戳...返回2009年1月22日0点0分0秒时间戳 二,获取英文文本日期时间 示例如下: 便于比较,使用date将当时间戳与指定时间戳转换成系统时间 (1)打印明天此时的时间戳strtotime(

    PHP时间戳格式全部汇总 (获取时间、时间戳)

    希望对新手的学习有所帮助! 一,PHP时间戳函数获取...便于比较,使用date将当时间戳与指定时间戳转换成系统时间 (1)打印明天此时的时间戳strtotime(”+1 day”) 当前时间:echo date(”Y-m-d H:i:s”,time()) 结果:2

    PHP时间戳和日期相互转换操作实例小结

    在php中我们要把时间戳转换日期可以直接使用date函数来实现,如果要把日期转换成时间戳可以使用strtotime()函数实现,下面我来给大家举例说明。 1. php中时间转换函数 strtotime(date("Y-m-d H:i")) date("Y-m-d H:i...

    php时间戳转换代码详解

    在php中我们要把时间戳转换日期可以直接使用date函数来实现,如果要把日期转换成时间戳可以使用strtotime()函数实现,下面我来给大家举例说明。 1.php中时间转换函数 strtotime (date()) date(Y-m-d H:i,$unixtime)...

    PHP时间戳与日期之间转换

    PHP时间戳与日期之间转换 echo(strtotime("now")); echo(strtotime("3 October 2005")); echo(strtotime("+5 hours")); echo(strtotime("+1 week")); echo(strtotime("+1 week 3 days 7 hours 5 seconds"));...

    PHP中strtotime函数使用方法分享

    1-22”) 示例如下: 1.echo strtotime(“2009-1-22”) 结果:1232553600 说明:返回2009年1月22日0点0分0秒时间戳 二,获取英文文本日期时间 示例如下: 便于比较,使用date将当时间戳与指定时间戳转换成系统时间 ...

    php强大的时间转换函数strtotime

    使用strtotime可以将各种格式的时间字符串转换为时间戳 转换常规时间格式 echo date('Y-m-d H:i:s', strtotime('2016-01-30 18:00')).PHP_EOL; echo date('Y-m-d H:i:s', strtotime('20160130180001')).PHP_EOL; ...

    PHP时间戳与日期之间转换的实例介绍

    1.php中时间转换函数 ...3.php中时间戳转换为日期,并按照时间显示不同的内容,如刚刚,分钟前,小时前,今天,昨天等 /*时间转换函数*/ function transTime($ustime) {   $ytime = date(“Y-m-d H:i”,$ust

    PHP 时间转换Unix时间戳代码

    复制代码 代码如下:&lt;... 您可能感兴趣的文章:php实现兼容2038年后Unix时间戳转换函数PHP中UNIX时间戳和日期间的转换与计算实例php 创建以UNIX时间戳命名的文件夹(示例代码)PHP+Mysql日期时间如何转换(UNIX时

    php中日期加减法运算实现代码

    1、首先通过strtotime()获得日期的时间戳 2、获得N天前得时间戳,通过”当前时间戳 – N天的秒数 = N天前得时间戳“ 3、对N天前得时间戳用date()函数进行格式转换 下例:获得2012-5-1号之前一天的日期 复制代码 代码...

    php strtotime 函数UNIX时间戳

    echo strtotime (“10 September 2000”), “\n”; echo strtotime (“+1 day”), “\n”; echo strtotime (“+1 week”), “\n”; echo strtotime (“+1 week 2 days 4 hours 2 seconds”), “\n”; e

    php中strtotime函数性能分析

    strtotime()是php中的时间函数;其功能是:将任何字符串形式的日期,时间转换成对应的Unix 时间戳。今天我们是通过具体的实例来详细分析下strtotime()函数的性能问题

    php获取当前月与上个月月初及月末时间戳的方法

    本文实例讲述了php获取当前月与上个月月初及月末时间戳的方法。分享给大家供大家参考,具体如下: 当前月 &lt;?...$thismonth = date('m'); $thisyear = date('Y');...$startDay = $thisyear ....$e_time = strtotime

    php 日期和时间的处理-郑阿奇(续)

    时间转化为时间戳 如果要将用字符串表达的日期和时间转化为时间戳的形式,可以使用strtotime()函数。 语法格式如下: int strtotime(string $time [, int $now ]) 例如: 复制代码 代码如下: &lt;?...

    PHP时间函数使用详解

    PHP开发中,关于时间函数的使用基本上可以说是无处不在,而PHP中操作时间的方法也很多,比如PHP时间戳、日期与时间戳之间的转换、获取当前日期、当前时间之前或之后的时间等等,下面我们详细讲述一下PHP中各种时间...

    php实现兼容2038年后Unix时间戳转换函数

    function fun_strtotime($var1=0,$var2=0){  if(!$var2){  $var2 = $var1;  $var1 = 0;  }  if(is_numeric($var2)){  $var2 = ‘@’.$var2;  }  try{  $date = new DateTime($var2);  $date-&gt;...

Global site tag (gtag.js) - Google Analytics