我有一串日期:
25/08/2012
我在这里尝试将其转换为 DateTime 对象,以便将其保存到 MySQL 数据库中。我的后端架构有一个准备接收此数据的 DateOfPrint 日期
列。
这是我尝试过的:
$eventDate = DateTime::createFromFormat('d/m/y', $fecha->innertext);
echo $eventDate;
echo
语句在屏幕上没有显示任何内容,并且在尝试将其保存到数据库时,该列中没有保存任何内容。
有什么建议吗?
最佳答案
您的 $eventDate
包含一个打印为空字符串的 bool 值 (false)。
您需要使用大写的 Y。
Y A full numeric representation of a year, 4 digits Examples: 1999 or 2003
y A two digit representation of a year Examples: 99 or 03
你必须调用DateTime::format() ;
例如
<?php
$fecha = new StdClass;
$fecha->innertext = '25/08/2012';
$eventDate = DateTime::createFromFormat('d/m/Y', $fecha->innertext);
if ( false===$eventDate ) {
die('invalid date format');
}
echo $eventDate->format('Y-m-d');
打印
2012-08-25
关于php - 使用 DateTime::CreateFromFormat 创建日期对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12074359/