0

I am using MsSQL in PHP. I am storing the time in database table row with datetime as datatype.The time is stored like this :08:30:00.0000000.I need the time to be displayed in 08:30.I have used

date('h:i', $time_value); // $time_value stores the time value

This formats the date and gives the result in 4:00. Any formatting is required to display the correct time stored in database?

hakre
  • 193,403
  • 52
  • 435
  • 836
Techy
  • 2,626
  • 7
  • 41
  • 88
  • you can refer this question http://stackoverflow.com/questions/13363647/trouble-using-mssql-datetime-in-php-via-sqlsrv – skstar May 04 '14 at 05:18

2 Answers2

2

The PHP date function does not expect a Mysql datetime as second parameter but you do so:

date('h:i', $time_value);
                 ^
                 |
          second parameter

This is the reason why it does not work. You're using the wrong value, convert the database value into a timestamp first because the date function needs a timestamp. As you migh imagine, this has been done before, here is just a selection of related Q&A material:

Alternatively just use a string function like substr to obtain the string you're looking for:

$time_value = '08:30:00.0000000';
echo substr($time_value, 0, 5); # 08:30

Demo: https://eval.in/146148

You can do the conversion / formatting with the SQL statement already, a related example is given in:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Fix the problem at the first place possible, here, select the data in the format you need it from the database already. If you share the SQL code you're using (at least in excerpt), I could have given more speaking example codes in the answer. – hakre May 04 '14 at 05:26
1

You can also use:

date('h:i', strtotime('08:30:00.0000000'));

In your case:

date('h:i', strtotime($time_value));
pah
  • 4,700
  • 6
  • 28
  • 37