2

im trying to display date and time from MSSQL Server datetime table using PHP, the value from from SQL Server is datetime EX. 2012-08-20 06:23:28:214. Now i want to display it the exact result but it php displays it like this EX. Aug 20 2012 6:23AM. I Have tried to use strtotime but the milliseconds does not display or rather display :000.

Any Help is so much appreciated. Thanks in Advance.

3
  • Judging by the yyyy-mm-dd date format, I am assuming you mean "MySQL server" when you say "SQL Server", which implies "MS SQL Server" -- an entirely different product. Commented Aug 26, 2012 at 22:58
  • sorry about that, that should be MS SQL SERVER.. Commented Aug 26, 2012 at 23:03
  • 1
    @ytse_jam It should be MS SQL Server? I am slightly confused, if it isn't mysql, could you remove it from the title and tags - I would do it, but don't want to remove the wrong tags. Commented Aug 26, 2012 at 23:07

3 Answers 3

1

Try this,

SELECT CONVERT(VARCHAR(50), CAST('2012-08-20 06:23:28:214' as DateTime), 100) 
          AS [OutPut]

-- expected output
--     Aug 20 2012 6:23AM

from the example above, I used CAST in order to convert string date into the original DateTime datatype. But in the original query, you can omit the CAST function since you've mentioned that the column is already on the DateTime data type.

SELECT CONVERT(VARCHAR(50), columnDateTime, 100)

SQLFiddle Demo

How to Format Date and DateTime in MSSQL Server

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

1 Comment

hi john, thank you so much. the select convert query did the trick. thanks again.
1

Use PHP's strtotime() to convert the sql date into unix time, then use date() to reformat it however you want. =strtotime("2012-08-20 06:23:28.214") works, so it's just a matter of using string manipulation to change that final colon into a decimal point.

Alternately, this blog post has code that you can use to have MS SQL Server return the unix time directly. The key function is SELECT DATEDIFF(second, '1970/01/01 00:00:00', @date).

If you are in fact using MySQL and not SQL Server, you can just use the function UNIX_TIMESTAMP() to return the current unix time.

Comments

0

The DateTime object has the input string type of u which represents microseconds (up to six of them).

DateTime::format does however use the same strings as the standard date But the following is taken from the docs about the u in outputting the data:

Microseconds (added in PHP 5.2.2). Note that date() will always generate 000000 since it takes an integer parameter, whereas DateTime::format() does support microseconds.

1 Comment

thanks for the answers, just to make it more clearer, im querying to MSSQL SERVER using PHP of course. the actual data from the MSSQL SERVER is like this "2012-08-20 06:23:28:214", now if i echo it, the output is like this "Aug 20 2012 6:23AM". now what i want is to echo the actual data. as i said earlier i tried using strtotime but the milliseconds echoes :000.

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.