PHP中_时间函数 date()

常用时间函数:

1. string date(string format,inieger timestamp)

举例:显示 2010年10月01日 对应的模板代码为  <!--{date('Y年m月d日',$time)}-->

细心的一定发现了 Y年m月d日中 Y对应的是2010,m为10,d为01。当然还有其它更多的格式,具体可以参考下面的对照表:

 

格式字符描述返回值
------
d月份中的天数,个位数带001 到 31
D3个字母的英文天数Mon 到 Sun
j月份中的天数,个位数不带0到 31
l (小写的'L')英文星期的单词Sunday 到 Saturday
NISO-8601 标准的星期几数( PHP 5.1.0中增加的)1 (代表星期一) 到7 (代表星期天)
SEnglish ordinal suffix for the day of the month, 2 charactersst, nd, rd or th. Works well with j
w数字代表星期几0 (星期天) 到6 (星期六)
z一年中的第几天 (从0开始)0 到365
星期------
WISO-8601标准一年中的第几个星期 ( PHP 4.1.0增加)例如: 42 (一年的第42个星期)
------
F英文的月份January 到 December
m月份的数字 个位数带001 到 12
M英文月份的缩写Jan 到 Dec
n月份的数字 个位数不带01 到12
t指定月份的天数28 到31
------
L是否是闰年1 为闰年, 0 为否.
oISO-8601 标准年格式 与Y相同. (added in PHP 5.1.0)比如: 1999 或者2003
Y4位的年份比如: 1999 或者2003
y2位数的年份比如: 99 或者03
时间------
a小写的午前、午后am 或者 pm
A大写的午前、午后AM 或者PM
B互联网时间000 到999
g12小时制的数字时间 个位不带01 到12
G24小时制的数字时间 个位不带00 到23
h12小时制的数字时间 个位带001 到12
H24小时制的数字时间 个位带000 到23
i分钟的数字 个位带000 到59
s分秒数字 个位带000 到 59
u分秒数 (PHP 5.2.2增加)比如: 654321
时区------
e时区标示 (PHP 5.1.0增加)比如: UTC, GMT, Atlantic/Azores
I (capital i)Whether or not the date is in daylight saving time1 if Daylight Saving Time, 0 otherwise.
ODifference to Greenwich time (GMT) in hoursExample: +0200
PDifference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3)Example: +02:00
TTimezone abbreviationExamples: EST, MDT ...
ZTimezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.-43200 through 50400
Full Date/Time------
cISO 8601 date (added in PHP 5)2004-02-12T15:19:21+00:00
r» RFC 2822 formatted dateExample: Thu, 21 Dec 2000 16:01:07 +0200
USeconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

2. array getdate(integer timestamp)

该函数返回一个矩阵.

如:

<?

$current_date=getdate();

print($current_date("hours"));

print($current_date("minutes");

print($current_date("seconds");

?>

说明:

元素 描述

hours 24小时格式的小时

mday 月份中日期

minutes 分钟

mon 数字形式的月份

month 月份全称

seconds 秒数

wday 从0到6的数字形式的星期几

weekday 星期几的名称

year 年份

0 时间戳即从1970年1月1日到现在的秒数

yday 一年中数字形式的日期

3. boolean checkdate(integer month,integer day,integer year)

该函数检查日期是否合法.如:

<?

if(checkdate(2,29,1980))

print("日期合法!n");

?>

4. integer time()

该函数获得当前时间戳.如:

<?

print(time());//输出一大串整数

?>

5. integer mktime(integer hour,integer minutes,integer seconds,integer month, integer day,integer year)

该函数返回给出日期的时间戳,即从1970年1月1日到现在的秒数.

如某参数超出范围,该函数也可以解释它,如13月即为第二年的一月.

如:

<?

$currenthour=date("H");

print("50个小时后为:");

print(date("h:i A l F dS,Y",mktime($currenthour+50)));

print("<br>n");

?>

6. string microtime()

该函数返回一个字符串,由当前时间的毫秒数+空格+从1970年开始的秒数

<?

print("start:microtime()<br>n");

for($index=0;$index<1000;$index++)

print("good!");

print("stop:microtime()<br>n");

?>

7. strtotime(time,now)

strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳。

<?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"));
echo(strtotime("next Monday"));
echo(strtotime("last Sunday"));
?>

 

 

戳。以下是一个简单的示例:// 设定要用的默认时区。从PHP5.1后可用
date_default_timezone_set('Asia/Chongqing');//中国地区使用
echo date("Y-m-d H:i:s");//显示当前时间
// 简单实现时间加减
$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));//明天
//现在的结果是时间戳,格式化显示,下面相同
echo date("Y-m-d H:i:s",$tomorrow);
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));// 下个月
$nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);// 明天
// 返回当前的 Unix 时间戳
echo time();
// 返回当前 Unix 时间戳和微秒数 microtime()
function microtime_float() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time_start = microtime_float(); // Sleep for a while
usleep(100);
$time_end = microtime_float();
$time = $time_end - $time_start;//程序执行时间 

 


简单的时间比较函数
<?php

//简单的时间比较函数,用于对不同日期进行比较

//strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳。

//语法:strtotime(time,now) ; time 规定要解析的时间字符串。

//now 用来计算返回值的时间戳。如果省略该参数,则使用当前时间。


$timer="2005-11-19 08:34:55";
$timer2="2005-11-18 05:34:55";

//注意时间格式要正确。

//第一种方法
function comparetime1($time1,$time2)
{
if(strtotime($time1)>strtotime($time2))
return "true";
else
return "false";
}
echo comparetime1($timer,$timer2);

//第二种方法
function comparetime2($time1,$time2)
{
$str=array(&apos-&apos,&apos&apos,&apos:&apos);
$rp1=str_replace($str,"",$time1);
$rp2=str_replace($str,"",$time2);
if($rp1>$rp2)
return "true";
else
return "false";
}
echo comparetime2($timer,$timer2)."
";
?>

 

 

修改从mysql中获取的datetime格式

    date函数可以设置日期格式,但是其参数只能是时间戳,对于mysql中的datetime格式,需要用strtotime()函数转换成时间戳。 

date( 'm-d   H:i ',strtotime($date));

 

这个我建议数据库中存储日期的格式为:int(10)考虑到uchome都是这么存储的我也是这么弄得,后来明白为什么这么做了

这是因为 $time 为存储的int(10) 数据可以通过PHP的date函数得到你想要的任何形式的时间

比如LZ要求的 $time1 = date('Y-m-d',$time);   2010-09-13
或者也可以是 $time2 = date('Y年m月d日 H时i分s秒',$time);  2010年9月13日 11时 52分 12秒 等等

总之存储Unix时间戳是最灵活的 也是最精确的

 

PHP服务器时间差8小时解决方案

date(‘Y-m-d H:i:s’)

结果取出来显示的时间总是于本地时间相差8个小时,我的本机是中国标准时间(GTM+8),问题应该是date()取的时间是格林威治时间,那应该怎么配置一下PHP服务器呢?

下面是我在网上找到的解决方法

问题原因所在
从php5.1.0开始,php.ini里加入了date.timezone这个选项,默认情况下是关闭的
也就是显示的时间(无论用什么php命令)都是格林威治标准时间
和我们的时间(北京时间)差了正好8个小时。

有以下3中方法可以恢复正常的时间。
1、最简单的方法就是不要用php5.1以上的版本
2、如果一定要用,而且不能修改php.ini,则需要在关于时间的初始化的语句的
上面加上 date_default_timezone_set (‘XXX’);
3、一劳永逸,仅限能修改php.ini。打开php.ini查找date.timezone 去掉前面的分号
date.timezone = PRC,重启http服务(如apache2或iis等)即可
【相关注解:】↓
关于XXX,大陆内地可用的值是:Asia/Chongqing ,Asia/Shanghai ,Asia/Urumqi (依次为重庆,上海,乌鲁木齐)
港台地区可用:Asia/Macao ,Asia/Hong_Kong ,Asia/Taipei (依次为澳门,香港,台北)
还有新加坡:Asia/Singapore
老外好像把北京漏调了
其他可用的值是:Etc/GMT-8 ,Singapore ,Hongkong ,PRC
PRC是什么?PRC是中华人民共和国啊-_-
你可以到http://www.php.net/docs.php上查到更多的地区
由于程序最后还是会把地名转为时区来计算,所以当你使用的不是内置的区域的时候,程序将自动使用格林威治标准时间。

我的笔记
如果没有修改php.ini的权限,那么应该在调用date()方法之前加上date_default_timezone_set(‘PRC’);
参数要加上双引号或单引号

修改php.ini时,
date.timezone = PRC
后面的参数不需要使用引号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值