我有一个表,其中列日期设置为日期时间。当我返回并从行中获取日期时,它只返回日期而不是时间。
$date = $row["date"];
我尝试如下格式化,但出现以下错误:
警告:date_format() 期望参数 1 为 DateTime,给定字符串
$date = date_format($row["date"], 'Y-m-d H:i:s');
我如何获得全部价值?
在您的选择语句中,将日期转换为日期时间。前任
SELECT CAST(date AS DATETIME) newDate
并将其检索为
$dateTime = strtotime($row["newDate"]);
您需要将字符串 $date 转换为正确的类型。我必须尝试一些事情才能做到这一点,但这现在在我的机器上运行:
$thisDate = "2013-02-02 22:17:06"; // example you gave, as a string
$timezone="America/New_York"; // machine was complaining when I didn't specify
$DT = new DateTime($thisDate, new DateTimeZone($timezone)); // this really is a DateTime object
echo $DT->format('Y-m-d H:i'); // you can echo this to the output
$dateString = $DT->format('Y-m-d H:i:s'); // or format it into a string variable
尝试:
$date = date_format(new DateTime($row['date']), "Y-m-d H:i:s");
或者
$date = date('Y-m-d H:i:s', strtotime($row['date']));
您需要先将字符串转换为日期类型。然后 date_format() 将起作用。试试下面的。
$date = date_format(date_create($row["date"]), 'Y-m-d H:i:s');
祝你好运