PHP5 and extracting MS SQL Server DateTimeStamp from table

Hello folks,

     I have a MS Sql Server table. One of the columns is a DateTime stamp.
I need only to extract the date. When I use the code:

$hiredate = $row["HireDate"];

Open in new window

gives me can "not convert to string" which I understand.
I need to only pull the date and not time, I know I can reformat the date once I successfully
assign it to a variable but that's the problem -
How do I assign it to a variable?  Can I reformat the date before assigning it to the variable?
I see all sorts of info about date/time in the php manual but they are using constants, not something pulled
from a table...
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Peos JohnPHPCommented:
You can use explode function in php for this...

for example, if

 $hiredate= '1970-01-01 00:00:01';

// you can explode this using space and this will return time and date separately in an array

print_r(explode(' ',$hiredate));

Hope this helps.
Dave BaldwinFixer of ProblemsCommented:
I don't understand because almost everything that comes back from a database is in text format unless it is in one of the binary data types.  Can you show us the CREATE TABLE or structure definition?
Dave BaldwinFixer of ProblemsCommented:
Turns out... you're right it doesn't work.  However... here is code that does work.  'tid' is an int, tdate1 is a small datetime, and tdate2 is a regular datetime.  'timestamp' is linked because in SQL Server, a 'timestamp' data type is NOT a Unix timestamp, it is just a 64 bit integer that has nothing to do with time.
$serverName = "xxxxxxxxx";    // Your database server
/* Get UID and PWD from application-specific files.  */
$uid = "xxxxxx";      // Your db username
$pwd = "xxxxxx";      // Your db password
$connectionInfo = array( "UID"=>$uid,
/* Connect using SQL Server Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
     echo "Unable to connect.</br>";
     die( print_r( sqlsrv_errors(), true));

echo '<table summary="" border="1px">';
// CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
// timestamp -
$dquery =  "SELECT tid, CONVERT (varchar,tdate1,0), CONVERT (varchar,tdate2,0) FROM dbo.tdatetest";
$result = sqlsrv_query($conn, $dquery); 
while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_NUMERIC)) {
echo '<tr bgcolor="#ffffff">';
echo '<td width="44px">&nbsp;'.$row[0].'</td>';
echo '<td>&nbsp;'.$row[1].'</td>';
echo '<td>&nbsp;'.$row[2].'</td>';
echo '</td></tr>';
echo "\r\n";


Open in new window

OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

MurfurFull Stack DeveloperCommented:
You can either format it in the SQL query or, more practically in my mind, format it in the PHP
-- I say more practically as you may find that you need to use the same query on another page but with a different format on the output.

<?= date( format, timestamp ); ?>

<?= date( "dS M Y", strtotime( $row["HireDate"] ) ) ; ?>
would give you 20th Nov 2015

<?= date( "d/m/y", strtotime( $row["HireDate"] ) ) ; ?>
would give you 20/11/15

See for the formatting options

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Handling date and time values in PHP and MySQL:

I am pretty sure there is a very similar set of constructs you can use with MS Sql Server.  The PHP will be mostly identical, and the principles will be the same.

You can learn a lot about your data if you do something like this:
var_dump( $row["HireDate"] );
OverthereAuthor Commented:
You have a point Mufur - converting within a sql query is very possible and I think more feasible.
 I can also convert it to a date in php for date comparisons.
Thank you everyone for responding, its as always appreciated...
OverthereAuthor Commented:
Just thought I would share this snippet.
This is the sql query that worked very well for me....

SELECT LEFT(CONVERT(VARCHAR, HireDate, 120), 10)as HireDate, Paid FROM MyTable WHERE RecId = 1001

Open in new window

Dave BaldwinFixer of ProblemsCommented:
The SQL Query that worked so well for is not shown in your Accepted Solution while a similar solution is shown in my post above.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.