Jump to content

Reading Date/Time values from a DB


jbmoyer

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/140167-reading-datetime-values-from-a-db/
Share on other sites

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']));

 

 

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);
}

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'])));
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.