php strtotime 转换错误的日期
我想知道为什么将日期转换为以秒为单位的时间,然后再转换回来,结果是错误的???
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你需要这个
结果是:
UNIX时间:1277267278
可读日期:2010-06-23 09:57:58
有效期: 2010-12-30 09:57:58
纪元:1293683278
阅读过期日期:2010-12-30 09:57:58
You need this
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