我需要从SQL Server的datetime
中获取日期,并格式化日期。(例如:01-Jan-2000
) 我正在使用PHP。
目前,SQL返回的是2009-02-13 22:00:00.000
。
我需要从SQL Server的datetime
中获取日期,并格式化日期。(例如:01-Jan-2000
) 我正在使用PHP。
目前,SQL返回的是2009-02-13 22:00:00.000
。
echo date('d-M-Y', strtotime($datetime_from_db));
$datetime_from_db
是查询返回的日期列。有关strtotime()
可以接受哪些字符串的示例,请参见手册页面:http://php.net/manual/en/function.strtotime.php。有关使用`date()`重新格式化日期的示例(例如Dan Grossman的示例),请参见http://us.php.net/manual/en/function.date.php。 - bob-the-destroyerstrtotime
解析的支持格式,所以他需要从数据库中取出日期的子字符串。 - Dan Grossmanstrtotime()
返回一个 int
,所以即使它正确解析了 OP 的日期,它也不会返回微秒。 - bob-the-destroyer你可以直接从mySQL查询中以任何格式汇总数据,
SELECT DATE_FORMAT(column_name, '%d-%b-%Y') FROM tablename
connectionInfo => 'ReturnDatesAsStrings'=>true
$connectionInfo = array( "Database"=>"AdventureWorks", 'ReturnDatesAsStrings '=> true);
strtotime()
能够正确地解析这个日期,尽管它会丢弃微秒部分。如果直接对这个字符串使用date()
函数是不可能的,因为date()
需要一个整数时间戳……奇怪的是,即使date()
无法接受或解析浮点数,它也有一个u
格式来返回微秒。玩呢去吧。 - bob-the-destroyer