PHP日期和时间函数之系统时区设置
第一种:
修改PHP.ini文件中的设置,找打[data]下的;date.timezone = 选项,去掉前面的引号,修改为:"date.timezone = Asia/Shanghai",然后重新启动Apache服务器
第二种:
date_default_timezone_set(timezone);
<?PHP header('Content-Type:text/html;charset=utf-8'); echo 'utc时间:'.date('Y-m-d H:i:s').'<br>'; date_default_timezone_set('PRC'echo '北京时间:'.date('Y-m-d H:i:s');
运行的结果为:
utc时间:2020-05-22 01:40:22
北京时间:2020-05-22 09:40:22
PHP time()函数获取当前时间戳实例详解
PHP中,通过time()函数获取当前的UNIX时间戳,返回值是从时间戳纪元(格林威治时间1970年1月1日 00:00:00)到当前的秒数。
获取当前时间戳实例
在本实例中,我们使用time()函数获取当前本地时间戳,并将时间戳进行格式化输出。代码如下
<?PHP ); echo '时间戳:'.time().'<br>'; echo '格式化时间:'.date('Y-m-d H:i:s',time()).'<br>'; $nexttime = time()+(7*24*60*60echo '一周后:'.$nexttime);
代码运行结果如下:
时间戳:1590114337 格式化时间:2020-05-22 02:25:37 一周后:2020-05-29 02:25:37
PHP date()函数获取当前日期和时间实例详解
date函数实例一
<?echo 'now: '.date('Y-m-d').'<br>';
代码运行结果:
now: 2020-05-22
date函数实例二
<?echo 'DATE_ATOM: '.date(DATE_ATOM).'<br>'echo 'DATE_COOKIE: '.echo 'DATE_ISO8601: '.echo 'DATE_PRC822: '.echo 'DATE_PRC850: '.echo 'DATE_RSS: '.echo 'DATE_W3C: '.date(DATE_ATOM).'<br>';
代码运行结果:
DATE_ATOM: 2020-05-22T02:29:49+00:00 DATE_COOKIE: 2020-05-22T02:29:49+00:00 DATE_ISO8601: 2020-05-22T02:29:49+00:00 DATE_PRC822: 2020-05-22T02:29:49+00:00 DATE_PRC850: 2020-05-22T02:29:49+00:00 DATE_RSS: 2020-05-22T02:29:49+00:00 DATE_W3C: 2020-05-22T02:29:49+00:00
PHP getdate()函数获取日期信息实例详解
getdate()函数返回的关联数组元素说明
getdate()函数实例
实例一
<?print_r(getdate());
运行结果如下图:
实例二
使用getdate()函数获取系统当前的日期信息,并输出该函数的返回值,实例代码如下:
<?); $arr = getdate(); echo $arr['year'].'/'.$arr['mon'].'/'.$arr['mday'].' '$arr['hours'].':'.$arr['minutes'].':'.$arr['seconds'].' '.$arr['weekday'].'<br>'echo 'today is the '.$arr['yday'].' day of year';
运行结果如下图:
2020/5/22 2:34:15 Friday
today is the 142 day of year
PHP checkdate()函数检验日期的有效性实例详解
checkdate()函数语法:
checkdate()函数是检查日期的有效性。
<?$year = 2020$month = 4$day = 30var_dump(checkdate($month,1)">$day,1)">$year)); $day = 31$year));
运行结果如下图:
注意参数顺序
也可以直接在checkdate()函数里面写日期
<?checkdate(5,20,2020));
PHP strtotime()函数将日期和时间解析为UNIX时间戳实例详解
strotime()函数实例
实例一
使用strotime()函数获取指定日期的unix时间戳,代码如下
<?echo strtotime('2020-5-20');
运行结果:
1589932800
实例二
本实例应用strotime()函数获取英文格式日期时间字符串的UNIX时间戳,并将部分时间输出,实例代码如下
<?strtotime('2020-5-22');echo '<br>'strtotime('now');strtotime('22 May 2020');strtotime('+7 day');strtotime('+1 week');strtotime('+1 week 2 days 3hours 4seconds');strtotime('next Friday');strtotime('last Friday');
代码运行结果:
1590105600 1590115279 1590105600 1590720079 1590720079 1590903683 1590710400 1589500800