当日期列为空时,PHP strtotime 返回 1970 年的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7737363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP strtotime returns a 1970 date when date column is null
提问by Sandy505
I want to display $row->depositdate in dd-mm-yyyy format.
我想以 dd-mm-yyyy 格式显示 $row->depositdate 。
If the date column in database is null the date displayed is : 01-01-1970
如果数据库中的日期列为空,则显示的日期为:01-01-1970
echo "<td align=center>".date('d-m-Y', strtotime($row->depositdate))."</td>";
If the date is null in database it should display nothing , otherwise the date in dd-mm-yyyy format should be displayed.
如果数据库中的日期为空,则不应显示任何内容,否则应显示 dd-mm-yyyy 格式的日期。
Thanks in advance
提前致谢
Sandeep
桑迪普
回答by GolezTrol
NULL is interpreted as 0 by strtotime, since it want to be passed an integer timestamp. A timestamp of 0 means 1-1-1970.
NULL 被 strtotime 解释为 0,因为它想要传递一个整数时间戳。时间戳 0 表示 1-1-1970。
So you'll have to check for yourself if $row->depositdate === NULL
, and if so, don't call strtotime at all.
因此,您必须自己检查 if $row->depositdate === NULL
,如果是,则根本不要调用 strtotime 。
回答by Ed Heal
NULL
is converted to 0 - the epoch (1-1-1970)
NULL
转换为 0 - 纪元 (1-1-1970)
Do this instead
改为这样做
echo "<td align=center>".($row->depositdate ? date('d-m-Y', strtotime($row->depositdate)) : '')."</td>";
回答by corretge
You need to check if $row->depositdata is_nullpreviously or check for 0 after strtotime if the value of $row->depositdata is unrecognizable for strtotime.
如果 $row->depositdata的值对于 strtotime 无法识别,您需要检查 $row-> depositdata之前是否为is_null或在 strtotime 之后检查 0。
echo "<td align=center>";
if (!is_null($row->depositdate))
{
$jUnixDate = strtotime($row->depositdate));
if ($jUnixDate > 0)
{
echo date('d-m-Y', $jUnixDate);
}
}
echo "</td>";
strtotimeexpects to be given a string containing an English date format and will try to parse that format into a Unix timestamp(the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
strtotime期望得到一个包含英文日期格式的字符串,并将尝试将该格式解析为Unix 时间戳(自 1970 年 1 月 1 日 00:00:00 UTC 以来的秒数),相对于 now 中给出的时间戳,或如果没有提供当前时间。
more about unixtime and Y2K38 problem: http://en.wikipedia.org/wiki/Year_2038_problem
有关 unixtime 和 Y2K38 问题的更多信息:http: //en.wikipedia.org/wiki/Year_2038_problem
回答by Param Lowe
Oh! I know why this happens? Simply you have not included "depositdate" in your SELECT query. Firstly Change SQL query to select all with wild card sign as shown here
哦!我知道为什么会这样?只是您没有在 SELECT 查询中包含“存款日期”。首先更改 SQL 查询以选择所有带有通配符的符号,如下所示
$sql = "SELECT * FROM `yourtable`";