转换后 PHP 日期显示“1970-01-01”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8984595/
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 date showing '1970-01-01 ' after conversion
提问by AssamGuy
I have a form in which date format is dd/mm/yyyy
. For searching database , I hanverted the date format to yyyy-mm-dd
. But when I echo
it, it showing 1970-01-01
. The PHP code is below:
我有一个日期格式为dd/mm/yyyy
. 为了搜索数据库,我将日期格式转换为yyyy-mm-dd
. 但是当我echo
它时,它显示1970-01-01
. PHP代码如下:
$date1 = $_REQUEST['date'];
echo date('Y-m-d', strtotime($date1));
Why is it happening? How can I format it to yyyy-mm-dd
?
为什么会发生?我怎样才能将其格式化为yyyy-mm-dd
?
回答by Cyclonecode
Replace /
with -
:
替换/
为-
:
$date1 = strtr($_REQUEST['date'], '/', '-');
echo date('Y-m-d', strtotime($date1));
回答by Oldskool
January 1, 1970 is the so called Unix epoch. It's the date where they started counting the Unix time. If you get this date as a return value, it usually means that the conversion of your date to the Unix timestamp returned a (near-) zero result. So the date conversion doesn't succeed. Most likely because it receives a wrong input.
1970 年 1 月 1 日是所谓的 Unix 纪元。这是他们开始计算Unix 时间的日期。如果您将此日期作为返回值,通常意味着将您的日期转换为 Unix 时间戳会返回(接近)零结果。所以日期转换不成功。很可能是因为它收到了错误的输入。
In other words, your strtotime($date1)
returns 0, meaning that $date1
is passed in an unsupported format for the strtotime function.
换句话说,您strtotime($date1)
返回 0,这意味着它$date1
以不支持的格式传递给 strtotime 函数。
回答by Kaushal Roy
$inputDate = '07/05/-0001';
$dateStrVal = strtotime($inputDate);
if(empty($dateStrVal))
{
echo 'Given date is wrong';
}
else{
echo 'Date is correct';
}
O/P : Given date is wrong
O/P : 给定日期错误
回答by Dnyaneshwar Harer
Another workaround:
另一种解决方法:
Convert datepicker dd/mm/yyyy
to yyyy-mm-dd
将日期选择器转换dd/mm/yyyy
为yyyy-mm-dd
$startDate = trim($_POST['startDate']);
$startDateArray = explode('/',$startDate);
$mysqlStartDate = $startDateArray[2]."-".$startDateArray[1]."-".$startDateArray[0];
$startDate = $mysqlStartDate;
回答by jerryurenaa
The issue is when your data is set to 000-00-00 or empty you must double-check and give the correct information and this issue will go away. I hope this helps.
问题是当您的数据设置为 000-00-00 或为空时,您必须仔细检查并提供正确的信息,这个问题就会消失。我希望这有帮助。
回答by Dayz
Use below code for php 5.3+:
对 php 5.3+ 使用以下代码:
$date = new DateTime('1900-02-15');
echo $date->format('Y-m-d');
Use below code for php 5.2:
对 php 5.2 使用以下代码:
$date = new DateTime('1900-02-15');
echo $date->format('Y-m-d');
回答by Tye Lucas
$date1 = $_REQUEST['date'];
if($date1) {
$date1 = date( 'Y-m-d', strtotime($date1));
} else {
$date1 = '';
}
This will display properly when there is a valid date()
in $date
and display nothing if not.
Solved the issue for me.
当有有效date()
输入时,这将正确$date
显示,否则不显示任何内容。
为我解决了这个问题。
回答by Bakhtawar GIll
finally i have found a one line code to solve this problem
最后我找到了一行代码来解决这个问题
date('d/m/Y', strtotime(str_replace('.', '-', $row['DMT_DATE_DOCUMENT'])));