PHP Cookbook读书笔记 – 第03章日期和时间

本章介绍了日期常用的函数及经常碰到的处理日期/时间的情景,如计算两个时间的差,还简单的介绍了不同时区间的日期显示问题。对于天文日历计算方面的(儒略日)转换函数,个人认为可以忽略。

strftime() : 根据区域设置setlocal()格式化时间/日期
date() :格式一个本地日期/时间
getdate() : 取得日期/时间(可以指定某时间戳)

Return array from getdate( )

Key

Value

seconds

Seconds

minutes

Minutes

hours

Hours

mday

Day of the month

wday

Day of the week, numeric (Sunday is 0, Saturday is 6)

mon

Month, numeric

year

Year, numeric (4 digits)

yday

Day of the year, numeric (e.g., 299)

weekday

Day of the week, textual, full (e.g., "Friday")

month

Month, textual, full (e.g., "January")

0

Seconds since epoch (what time( ) returns)

localtime() : 返回本地服务器的时间

Return array from localtime( )

Numeric position

Key

Value

0

tm_sec

Second

1

tm_min

Minutes

2

tm_hour

Hour

3

tm_mday

Day of the month

4

tm_mon

Month of the year (January is 0)

5

tm_year

Years since 1900

6

tm_wday

Day of the week (Sunday is 0)

7

tm_yday

Day of the year

8

tm_isdst

Is daylight savings time in effect?

mktime() : 取一个日期的unix时间戳,有6个参数时、分、秒、月、日、年
gmmktime() : 取GMT日期的unix时间戳
gregoriantojd() : 公历转儒略日(忽略)
checkdate() : 验证一个日期是否有效(例如可以判断4月31日为一错误日期)
date_default_timezone_set() : 设定用于脚本的默认时区
microtime() : 返回Unix时间戳和微秒数,用来计算程序的运行时间常用这个方法
cal_from_jd() : 转换Julian Day计数到一个支持的历法(忽略)
cal_to_jd() : 从一个支持的历法转变为Julian Day计数(忽略)
gregoriantojd() : 转变一个Gregorian历法日期到Julian Day计数(忽略)
Date_Calc () : pear的日历扩展类
putenv() : 配合localtime()通过设置一个时区的环境变量来获得当地时间(解决服务器和访问者不在同一个时区的情况)

<?php
$stamp_future = mktime(19,28,0,5,19,2011);
print strftime('%c',$stamp_future);	 
//输出:05/19/11 19:28:00

$a = getdate();
printf('%d-%d-%d %s:%s:%s',$a['year'],$a['mon'],$a['mday'],$a['hours'],$a['minutes'],$a['seconds']);
//输出:2011-5-19 11:32:18

// 7:32:56 pm on May 10, 1965
$epoch_1 = mktime(19,32,56,5,10,1965);
// 4:29:11 am on November 20, 1962
$epoch_2 = mktime(4,29,11,11,20,1962);
// 两个日期相差的秒数
$diff_seconds  = $epoch_1 - $epoch_2;
//相差的天数
$diff_days     = floor($diff_seconds/86400);

?>

儒略日(Julian day,JD)是指由公元前4713年1月1日,协调世界时中午12时开始所经过的天数,多为天文学家采用,用以作为天文学的单一历法,把不同历法的年表统一起来。如果计算相隔若干年两个日期之间的天数,利用儒略日就比较方便。

posted @ 2011-12-15 13:02  涂文瀚  阅读(494)  评论(0编辑  收藏  举报
foot