日期的计算

<?php
    $year=1985;
    $month=11;
    $day=13;
    $birthday=mktime(0,0,0,$month,$day,$year);
    $nowdate=mktime();
    $ageunix=$nowdate-$birthday;
    $age=floor($ageunix/(60*60*24*365));
    echo "年龄:".$age;
?>

出现警告:


Warning:  mktime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in C:\AppServ\www\ttt\index.php on line 5

Warning:  mktime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in C:\AppServ\www\ttt\index.php on line 6
年龄:33

日期的计算


解决办法:

原因分析:
  这是因为PHP默认是以格林威治标准时间(UTC)为准进行时间转换,因为自己所处的当地时间与UTC会有出入,格林威治标准时间和北京时间有8个小时的时间差。因此我们必须为PHP指定一个时区,让php知道以哪个时区为准进行转换。

解决办法: 

      在你的PHP程序头部使用date_default_timezone_set()设置我的默认时区为北京时间(PRC,中国的英文缩写), 即  <?php     date_default_timezone_set("PRC");    ?>就可以了。这种方法使用时太复杂,必须在每个使用date的文件中添加相关函数,下面的方法更简单方便。

在php.ini中找到date.timezone行,支持前面的';'号(如果没有修改过的话应该是没有启动此选项的),并将它的值设置为PRC,设置好以后为:date.timezone=PRC。

最后别忘了重新启动httpd服务喽,问题终于告一段落了。

修改后代码:

<?php  date_default_timezone_set("PRC"); 
    $year=1985;
    $month=11;
    $day=13;
    $birthday=mktime(0,0,0,$month,$day,$year);
    $nowdate=mktime();
    $ageunix=$nowdate-$birthday;
    $age=floor($ageunix/(60*60*24*365));
    echo "年龄:".$age;
?>

日期的计算

猜你喜欢

转载自blog.csdn.net/weixin_41737295/article/details/89317414