php 日期时间操作

1、date_default_timezone_get():返回默认时区
<?php

echo date_default_timezone_get();

//运行结果
UTC


2、date_default_timezone_set():设置默认时区
<?php
date_default_timezone_set('PRC');
echo date_default_timezone_get();

//运行结果
PRC


3、获取日期unix时间戳

<?php
echo time(); //获取当前时间时间戳
echo '<br/>';

echo strtotime('2017-10-19 19:36:52'); //获取指定日期时间字符串时间戳
echo '<br/>';

echo strtotime('2017-10-19');//获取指定日期时间戳,返回为2017-10-19 0:0:0时间戳
echo '<br/>';

//结果
1508413106
1508441812
1508371200



4、格式化日期时间
<?php
echo microtime();

//结果
0.11777200 1508422382



ps:关于date函数格式化参数的解释,可以参考php date函数定义


5、microtime():获取毫秒
<?php
echo microtime();

//结果
0.11777200 1508422382




6、getdate():
<?php

print_r(getdate());

//结果
Array
(
    [seconds] => 39
    [minutes] => 14
    [hours] => 14
    [mday] => 19
    [wday] => 4
    [mon] => 10
    [year] => 2017
    [yday] => 291
    [weekday] => Thursday
    [month] => October
    [0] => 1508422479
)




7、strtotime():将英文文本日期时间解析为 Unix 时间戳

<?php

echo date('Y:m:d H:i:s', time()); //格式化时间戳,获取当前日期时间字符串格式
echo '<br/>';

echo date('Y:m:d H:i:s', strtotime('-1 day')); //打印前一天
echo '<br/>';

echo date('Y:m:d H:i:s', strtotime('+1 day')); //打印后一天
echo '<br/>';

echo time();//获取当前时间戳
echo '<br/>';

echo strtotime("now");//同time()一样,获取当前时间戳
echo '<br/>';

echo strtotime("10 September 2000");//获取2000:09:10 00:00:00时间戳
echo '<br/>';
echo strtotime("+1 day");//获取后一天时间戳
echo strtotime("+1 week");//获取后一周时间戳
echo strtotime("+1 week 2 days 4 hours 2 seconds");//获取后一周+2天+4小时时间戳
echo strtotime("next Thursday");//获取下周2时间戳
echo '<br/>';echo strtotime("last Monday");//获取临近最后一个周一时间戳
echo '<br/>';
echo date( "Y-m-d", strtotime( "2009-01-31 +1 month" ) ); // PHP: 2009-03-03
echo date( "Y-m-d", strtotime( "2009-01-31 +2 month" ) ); // PHP: 2009-03-31

//在指定时间进行偏移
$t=1508489298;//指定时间戳
echo $dt=date('Y-m-d H:i:s',$t);//2017-10-20 08:48:18
echo '<br/>';
/*方法一*/
echo date('Y-m-d H:i:s',$t+1*24*60*60);//指定时间戳+1天 2017-10-21 08:48:18
echo '<br/>';
echo date('Y-m-d H:i:s',$t+365*24*60*60);//指定时间戳+1年 2018-10-20 08:48:18
echo '<br/>';
/*方法二*/
//$dt是指定时间戳格式化后的日期
echo date('Y-m-d H:i:s',strtotime("$dt+1day"));//指定时间戳+1天 2017-10-21 08:48:18
echo '<br/>';
echo date('Y-m-d H:i:s',strtotime("$dt+1year"));//指定时间戳+1年 2018-10-20 08:48:18
echo '<br/>';
/*方法三*/
//$t是指定时间戳
echo date('Y-m-d H:i:s',strtotime("+1day",$t));//指定时间戳+1天 2017-10-21 08:48:18
echo '<br/>';
echo date('Y-m-d H:i:s',strtotime("+1year",$t));//指定时间戳+1年 2018-10-20 08:48:18







                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值