1

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?

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

How would I go about doing it your way?
Nvm, figured it out. Works a charm!
1
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

Comments

1

Try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.