从DateTime对象中分隔日期属性
问题描述:
Unable to separate the date property from the DateTime object. What do I do?
I have used the following code:
while( $fetch = sqlsrv_fetch_array( $stmt , SQLSRV_FETCH_ASSOC) ) {
//echo '<pre>';
print_r($fetch); exit;
$date=$fetch['Dt_Installment_Date'];
echo '<pre>'; print_r($date); exit;
}
The current output that I am getting is as follows:
<pre>DateTime Object
(
[date] => 2019-01-18 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
I want to get only the [date] property and nothing else.
答
That's how you can format your DateTime object and convert into a String.
// Y : means year , m : means months , d : means days. and you can change format by seperating "/" or using same with "-"
$result = $date->format('Y-m-d');
echo $result;
Output is 2019-01-18