带有夏令时和其他变量的PHP mktime


PHP mktime with daylight savings and other variables

我使用mktime()将所有内容的所有日期存储在数据库中,我有一个脚本可以遍历并返回一定次数的日期,这可以很好地显示发布的时间,但当我试图返回确切的日期/时间时,它会提前一小时返回,所以我想知道,当时间因夏令时或其他原因而更改时,如何使日期始终返回正确的日期。

以下是我使用的基本内容(减少显示一小部分):

function time_stamp($session_time) 
{ 
    date_default_timezone_set("EST");
    $time_difference = time() - $session_time; 
    $seconds = $time_difference; 
    $minutes = round($time_difference / 60 );
    $hours = round($time_difference / 3600 ); 
    $days = round($time_difference / 86400 ); 
    $weeks = round($time_difference / 604800 ); 
    $months = round($time_difference / 2419200 ); 
    $years = round($time_difference / 29030400 );
    // Seconds
    if($seconds==0)
    {
         return $seconds." second ago";     //See this works fine, obviously
    }
        else if($seconds <=60&&$seconds>0)
    {
        return date('F jS 'a't g:ia ', $session_time+60*60); //it's when i use date()
                                                             //it doesn't
    }

简单的解决方案。。。

在php.ini文件中添加以下一行:

date.timezone = "America/New_York"

这样做之后,就不需要:

date_default_timezone_set("EST");

并使用以下功能:

time();

date();

应该不是问题。