getting date out of mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

getting date out of mysql

Post by yaron »

Hi all,
I have this starnge problem.
I try to get the datetime field out of mysql.
I use:
$arr=sql_fetch_array($result)
$date=$arr['mdate'];

This code used to work fine and I got the date in the format yyyy-mm-dd H:i:s just as it is in the db.
but now I use php 4.33 and I get just the year int the $date variable.
In the old version things still work good.
What is the problem?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

I guess that you made a typo; mysql_fetch_array?
Anyways, it sounds very wierd. How do you verify that its 'yyyy-mm-dd H:i:s' actually is stored in the database?
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

Post by yaron »

yea yea it's mysql_fetch_array() ofcourse.
in can see in the phpmyadmin that this field is entered correctly.
No difference from the other version...
quite a puzzle isn't it?!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

What do you get if you use print_r() on the row array, e.g.:

Code: Select all

$row = mysql_fetch_array($result);
echo '<pre>';
print_r($row);
echo '</pre>';
Does that date show up properly there? It may also be useful to see the SQL statement that you use to draw the information from the database.

Mac
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

Post by yaron »

It still prints only the year.
in the db it apears 2003-09-08 16:55:02
and the same code works in another version...
As for my sql statement it was just: SELECT * FROM table
nothing complex and should get all fields and it does but the date appears starnge!!
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

My guess would be something to do with conversion to an integer variable. PHP is not strongly typecast, but if you convert the string "2003-12-15 etc" into an interger variable and than back into a string you will get "2003" alone.

Not seeing all of your code is a handicap in diagnosing the problem, but look for anything that may be casting that element to an integer. Something in the more recent version may be more strongly typecasting than in the earlier version.
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

Post by yaron »

Ohhhhh...
I found my stupid mistake.....
The field type was decimal and not datetime (of course in the old system it was datetime).
Sorry to waste your time on such a stupid mistake.
and Thank you Bill H - your conversion idea made me look in the right place
Post Reply