Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.