【问题标题】:datetime php fatal error with negative time stamps带有负时间戳的datetime php致命错误
【发布时间】:2013-03-20 20:15:29
【问题描述】:

试图解析 1942 年 5 月 5 日

-872726549

DatTime::__construct()

Failed to parse time string (-872726549) at position 7 (4):
Unexpected character

1970 年 1 月 1 日之前的所有日期都相同

1969 年 12 月 31 日有同样的错误

我在 windows 上使用 xampp,php 5.4

这会触发错误:

$topv=-872726549;
$compared_time=new DateTime($topv);

【问题讨论】:

  • 您知道时间戳从 0 开始,到最大 2 的 32 次方结束吗?时间戳总是正整数。#
  • 我遇到了$date=$year.'-'.$month.'-'.$day.' '.date("H:i:s"); $datev=strtotime(date($date),time());的负值
  • 你读过 DateTime 的文档吗? public DateTime::__construct() ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] ) 并且还明确指出:time: 'A date/time string. Valid formats are explained in Date and Time Formats.'

标签: php datetime timestamp


【解决方案1】:

根据文档,您应该这样做:

$topv='1930-08-09';
$compared_time=new DateTime($topv);

【讨论】:

  • 对,应该是 $date=$year.'-'.$month.'-'.$day.' '.date("H:i:s"); $datev=strtotime(date($date),time()); $datev=date('Y-m-d H:i:s', $datev); 并且没有错误,谢谢,它与检查 strpos 的 - 并返回其他值而不是进行日期格式化的函数有关
  • 如果您需要两个日期之间的差异,您可以使用一些特定于 DateTime 的函数来做到这一点
猜你喜欢
  • 1970-01-01
  • 2013-04-18
  • 2012-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-09
相关资源
最近更新 更多