开发者

php strtotime convert wrong date

I wonder why after convert date to time in second, then convert back, result is wrong???

$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= conver开发者_开发知识库tFromDateToEpochTime($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;
}


You need this

$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;  

The result is: unix time: 1277267278
readable date: 2010-06-23 09:57:58
expirydate: 2010-12-30 09:57:58
epochtime: 1293683278
readexpirydate: 2010-12-30 09:57:58

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜