php⽉份转英⽂缩写,将⽇期转换成时间戳strtotime
1、sql:    localtime和now()返回当前⽇期与时间:2011-2-26 13:28:43        curtime()返回当前时间:13:28:43
curdate()返回当前⽇期:2011
2、sql:  date('2011-03-23')返回表达式中的⽇期:2011-03-23
hour(time) 返回time中的⼩时 hour(10:23:12)返回10;second(time)返回秒;minute(time)
DATEDIFF() 函数返回两个⽇期之间的天数。
DATEDIFF(date1,date2)
date1 和 date2 参数是合法的⽇期或⽇期/时间表达式。
注释:只有值的⽇期部分参与计算。
eg:返回最近⼀周上传的表
date_default_timezone_set('PRC');
$sql="select * from 1_1 where DATEDIFF(CURDATE(),uptime)<=7 ";
$result=mysql_query($sql) or die(mysql_error());
unix时间戳转换日期格式3、php:  在页头使⽤date_default_timezone_set()设置默认时区为北京时间
date_default_timezone_set('PRC');
echo date('Y-m-d H:i:s');
date_default_timezone_set('Etc/GMT-8');
echo time()."
";                  //1301569127
echo date("Y-m-d",time())."
";  //2011-03-31
echo date("Y-m-d H:i:s",time())."
"; //2011-03-31 18:58:47
echo date("Y-m-d H:i:s",time()+1*60*60)."
";//2011-03-31 19:58:47
echo date("Y-m-d H:i:s",time()-1*60*60)."
";//2011-03-31 17:58:47
>
strtotime能将任何英⽂⽂本的⽇期时间描述解析为Unix时间戳,我们结合mktime()或date()
格式化⽇期时间获取指定的时间戳,实现所需要的⽇期时间。
格式:int strtotime ( string $time [, int $now ] )
成功则返回时间戳,否则返回 FALSE。在 PHP 5.1.0 之前本函数在失败时返回 -1。
本函数预期接受⼀个包含美国英语⽇期格式的字符串并尝试将其解析为 Unix 时间戳(⾃ January 1 197
0 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-21 09: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-29 09: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-15 09: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()格式化⽇期时间获取指定的时间戳,实现所需要的⽇期时间。
下⾯这个例⼦实现给⼀个时间输出加四个⼩时后的功能
$time="2011-4-20 20:20:20";
$timestamp=strtotime($time);//⾸先转换成时间戳
$t=date("Y-m_d H:i:s",$timestatmp+4*60*60);
>
或者
$time="2011-4-20 20:20:20";
$timestamp=strtotime($time."+4 hour");//⾸先转换成时间戳$t=date("Y-m_d H:i:s",$timestatmp);
>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。