简体   繁体   中英

Formatting SQL Server time(7) using PHP date()

In my SQL Server database I have a time field stored as time(7). I have been trying to print this in PHP using echo but I wouldn't work, so I used this code

"ReturnDatesAsStrings" =>1

and it returned

08:35:00.0000000

And for the past hour I've been trying to format it into 08:35am using the date() function but have had no luck. Any clues?

If you disable ReturnDatesAsStrings what you get is a DateTime object. Obviously, a object cannot be printed with a plain echo, but you have a proper date and all you have to do is to call the format() method on it. Using strings as intermediate format when PHP can do it for you is a bigger hassle.

echo date('H:ia', strtotime('08:35:00.0000000')); // 08:35am

or, better, use the DateTime class:

$date = new DateTime('08:35:00.0000000');
echo $date->format('H:ia'); // 08:35am

尝试这个:

date_format( strtotime($datetime_field_in_db), 'g:i A') );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM