您的位置:首页 > 编程语言 > PHP开发

php学习笔记--关于时间戳

2017-01-28 18:14 701 查看
获取时间戳

int time ( void )

   返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数。 

mixed microtime ([ bool $get_as_float ] )

   microtime() 当前 Unix 时间戳以及微秒数。

   如果调用时不带可选参数,本函数以 "msec sec" 的格式返回一个字符串,其中 sec 是自 Unix 纪元

(0:00:00 January 1, 1970 GMT)起到现在的秒数,msec 是微秒部分。字符串的两部分都是以秒为单位

  返回的。 

  如果给出了 get_as_float 参数并且其值等价于 TRUE , microtime() 将返回一个浮点数。 

可以用microtime求一代码段的运算时间:

$strat = microtime(true);
//代码段;
$end = microtime(true);
echo $end-$strat;


int
mktime ($hour,$minute,$second,$month,$day,$year)
 
把指定时间转化为时间戳;参数定义的数值依次是 时分秒月日年  参数可以从右向左省略,任何省略的参数会被设置成本地日期和时间的当前值。 

用此方法可以计算两个日期的相差天数

$t=  (mktime(0,0,0,1,28,2017) -mktime(0,0,0,4,13,1995))/(3600*24);


int strtotime() 

把字符串转化为时间戳

1)打印指定日期的时间戳strtotime(”1995-04-13”)(注:此方法同mktime(0,0,0,4,13,1995);)

2)打印现在此时的时间戳strtotime(”now”)

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

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

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

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

时间戳格式化

date() —— 格式化一个本地时间/日期 

string date ( string $format [, int $timestamp ] )

返回将整数 timestamp 按照给定的格式字串而产生的字符串。如果没有给出时间戳则使用本地当前时间。

换句话说, timestamp 是可选的,默认值为 time() 。

注意:  date('y-m-d h:i:s 星期N',$time);  //注意,传入的第一个参数里面除控制字符外的字符原样输出,如:输出结果为2014-3-22  20:34:33 星期六  '-',':','星期'原样输出;  

从php5.10开始,php中加入了时区的设置,在php中显示的时间都是格林威治标准时间,这就造成了我们中国的用户会差八个小时的问题!

相关设置是修改php.ini中的 date.timezone 参数:

[Date]

; Defines the default timezone used by the date functions

;date.timezone =

默认是关闭的,只需把注释去掉,改为即可

[Date]

; Defines the default timezone used by the date functions

date.timezone = PRC

其中PRC是“中华人民共和国”!  

$time = time();
date_default_timezone_set('PRC');  //设置北京时区
echo date('Y-m-d H:i:s',$time);


检测某指定日期是否合法   
checkdate()

bool
checkdate ( int
$month
,
int
$day
,
int
$year
)

只能检测日期
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: