php strtotime 转换错误的日期

发布于 2024-10-08 21:12:42 字数 1256 浏览 3 评论 0原文

我想知道为什么将日期转换为以秒为单位的时间,然后再转换回来,结果是错误的???

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

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= 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯下孤影 2024-10-15 21:12:42

你需要这个

$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
有效期: 2010-12-30 09:57:58
纪元:1293683278
阅读过期日期:2010-12-30 09:57:58

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文