jbmoyer Posted January 9, 2009 Share Posted January 9, 2009 I have an Access DB Table and there is a field that is Date/Time format and stores the following: Field Name: TheDate 1/9/2009 1/5/2009 1/3/2009 etc... I have a SQL Call on my page to retrieve the data: $nTracker = "SELECT * from Tracker where TheDate = #" . $xcDate . "# Order by TrackerID DESC"; $TrackerData = odbc_exec($oConn, $nTracker); while ($row = odbc_fetch_array($TrackerData)) { print("<br />" . $row['TheDate']); } The problem is that the date that is displayed is renderd as follows: 2009-01-09 00:00:00 2009-01-05 00:00:00 2009-01-03 00:00:00 etc... I assume since it is a Date/Time field that is why PHP is converting it to a full date, how can I retrieve it so that I get what is Stored? 1/9/2009? Quote Link to comment https://forums.phpfreaks.com/topic/140167-reading-datetime-values-from-a-db/ Share on other sites More sharing options...
premiso Posted January 9, 2009 Share Posted January 9, 2009 date Use that on your output to contol what is displayed: while ($row = odbc_fetch_array($TrackerData)) { print("<br />" . date('Y-m-d', $row['TheDate'])); } Should print it out without the extra stuff. Quote Link to comment https://forums.phpfreaks.com/topic/140167-reading-datetime-values-from-a-db/#findComment-733457 Share on other sites More sharing options...
jbmoyer Posted January 9, 2009 Author Share Posted January 9, 2009 Works great thank you.....except I get the following in my response. Notice: A non well formed numeric value encountered in C:\TiaaWebs\St01\Docs\portal\index.php on line 60 12/31/1969 First the Date shoulnt be 1969 lol Second This is Line 60: echo(date('m/d/Y', $row['TheDate'])); Quote Link to comment https://forums.phpfreaks.com/topic/140167-reading-datetime-values-from-a-db/#findComment-733468 Share on other sites More sharing options...
premiso Posted January 9, 2009 Share Posted January 9, 2009 date Use that on your output to contol what is displayed: while ($row = odbc_fetch_array($TrackerData)) { print("<br />" . date('Y-m-d', $row['TheDate'])); } Should print it out without the extra stuff. Sorry: while ($row = odbc_fetch_array($TrackerData)) { print("<br />" . date('Y-m-d', strtotime($row['TheDate']))); } That would work, or a faster version, since strtotime is slow: while ($row = odbc_fetch_array($TrackerData)) { $dates = explode(" ", $row['TheDate']); $TheDate = $dates[0]; print("<br />" . $TheDate); } Quote Link to comment https://forums.phpfreaks.com/topic/140167-reading-datetime-values-from-a-db/#findComment-733469 Share on other sites More sharing options...
jbmoyer Posted January 9, 2009 Author Share Posted January 9, 2009 ok thank you they both work, but on the second one how can I format the date? I tried: date('m/d/Y',$TheDate) - - -But I get the 1969 one again... Thank you.... How much "slower" is the first? lol Quote Link to comment https://forums.phpfreaks.com/topic/140167-reading-datetime-values-from-a-db/#findComment-733473 Share on other sites More sharing options...
premiso Posted January 9, 2009 Share Posted January 9, 2009 while ($row = odbc_fetch_array($TrackerData)) { $dates = explode(" ", $row['TheDate']); $TheDate = str_replace("-", "/", $dates[0]); print("<br />" . $TheDate); } You could do that, or just simply use the strtotime one I posted, I think the above will be faster, but it is 2 extra lines of code, so yea. Strtotime method: while ($row = odbc_fetch_array($TrackerData)) { print("<br />" . date('Y/m/d', strtotime($row['TheDate']))); } Quote Link to comment https://forums.phpfreaks.com/topic/140167-reading-datetime-values-from-a-db/#findComment-733506 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.