php strtotime转换错误的日期

时间:2010-12-23 06:55:51

标签: date strtotime epoch

我想知道为什么在第二次转换日期之后,然后转换回来,结果是错误的???

$br= "<br>";

$unixtime= 1277267278;
echo 'unix time: ' . $unixtime . $br;

$readabledate= date('Y-m-d H:i:s', $unixtime);
echo 'readable date: ' . $readabledate . $br;

$expirydate= date('Y-m-d H:i:s', strtotime($readabledate . " +6 months + 7 days"));
echo 'expirydate: ' . $expirydate . $br;

$epochtime= convertFromDateToEpochTime($expirydate);   //convert to unixtime

echo 'epochtime: '. $epochtime . $br;

$readexpirydate= convertFromEpochTimeToDate($epochtime,$method = 1);  //convert it back to readable date

echo 'readexpirydate: '. $readexpirydate . $br;  // final result is wrong- 2009-11-03 xx

echo $br.$br.$br.$br.$br;
function convertFromDateToEpochTime($date)
{
        $year=substr($date,0,4);
        $month=substr($date,4,2);
        $day=substr($date,6,2);
        $hour=substr($date,8,2);
        $minute=substr($date,10,2);
        $second=substr($date,12,2);
        $newdate=mktime($hour,$minute,$second,$month,$day,$year);

        return $newdate;
}


function convertFromEpochTimeToDate($epoch,$method = 1)
{
 if ($method == 1)
    $date= date("Y-m-d H:i:s", $epoch);

 else if ($method == 2)
  $date= date("m/d/y H:i:s", $epoch);

 return $date;
}

1 个答案:

答案 0 :(得分:0)

你需要这个

$unixtime= 1277267278;
echo 'unix time: ' . $unixtime . $br;

$readabledate= date('Y-m-d H:i:s', $unixtime);
echo 'readable date: ' . $readabledate . $br;

$expirydate= date('Y-m-d H:i:s', strtotime($readabledate . " +6 months + 7 days"));
echo 'expirydate: ' . $expirydate . $br;

$epochtime= strtotime($expirydate);   //convert to unixtime

echo 'epochtime: '. $epochtime . $br;

$readexpirydate= date('Y-m-d H:i:s', $epochtime);  //convert it back to readable date

echo 'readexpirydate: '. $readexpirydate . $br;  

结果是: unix时间:1277267278
可读日期:2010-06-23 09:57:58
expirydate:2010-12-30 09:57:58
epochtime:1293683278
readexpirydate:2010-12-30 09:57:58