Forum Moderators: coopster

Message Too Old, No Replies

date() problem (unix)

         

firemaster

1:07 am on Jun 25, 2003 (gmt 0)

10+ Year Member



I am pulling a date of a db (the user's birthdate)

while($row = mysql_fetch_array($ms_query)) {
$user_bday = $row['birthdate'];
}

then I am splitting it up in order to be placed in 3 seperate fields (month, day, year), reasons I need to do this.

$user_month = date(m, $user_bday);
$user_day = date(d, $user_bday);
$user_year = date(Y, $user_bday);

Problem is the date always displays as 12-31-1969, went onto php.net and read about it being because it is on a unix system and that I have to make the '$user_bday' reconized as a date... question is how do I do this?
thanks,
-FM-

grahamstewart

1:18 am on Jun 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What column type have you used to store 'birthdate' in your database? (DATE / DATETIME / TIMESTAMP etc)

firemaster

1:34 am on Jun 25, 2003 (gmt 0)

10+ Year Member



It is stored in a DATE column 0000-00-00 by default.

This is what was on php.net:


GUGU_Brasil
19-Jun-2003 03:21

USE THIS IF U HAVE PROBLEM WHEN FORMATING DATE. IF YOUR SERVER IS WINDOWS, THE PHP WILL UNDERSTAND THE DATE FUNCTION OK. BUT IF YOUR SYSTEM WILL RUN ON A UNIX PLATAFORM, U HAVE TO TRANSLATE A STRING TO DATE SO THAT THE UNIX CAN UNDERSTAND THE DATETIME AS DATE.

0.PROBLEM: ECHOS WRONG DATE. (Ususally: 1969-02-12);

1.SOLVER:

//on RecordSets, lives the DateTime Field From your Database Table
$DATE_STR = $RECORD_SET->FIELD["DATE_TIME_FIELD"];

//use strtotime(); function to tell Server that this STR is a DATE. WORKS for UNIX SERVER.
$DATE = strtotime($DATE_STR);

//use date(); Then The Server Will Understand the DATE as DATE and format it as U desire.
$STR_OF_DATE = date("d-m-Y h:i:s",$DATE);

//I hope this could help,
//GUGU_BRASIL.

I tried this and it displayed today's date. I am not sure if I did something wrong?
-FM-

grahamstewart

2:00 am on Jun 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm.. not sure about that solution but here is how I handle it.

First I specify in my sql query how I would like the date formatted.


$sql = "select name, date_format(birthdate,'%d/%m/%y') as fbday from users"

..and then I use explode to get the date parts..


$dateparts = explode( $row['fbday'] );

Alternatively you could just specify the date field three times in your query...


$sql = "select name, dayofmonth(birthdate) as b_day, month(birthdate) as b_month, year(birthdate) as b_year from users"

.. and you'll get it already broken into day month and year fields for you.

firemaster

2:29 am on Jun 25, 2003 (gmt 0)

10+ Year Member



I'm sorry, this isn't the anwser I am really looking for, I don't want to format the date when I pull it out of of DB. This isn't the only place where I am displaying the date (and I am taking out more then just the date into an array), I need it to just not display as that wierd date and as the date from the db.

Also, I have never used the exlode () function so I don't know much about it.

If thier is no other way to get the desired results, then I guess am I going to have to format when I pull the info.. argh.

thanks for your help,
-FM-

firemaster

2:49 am on Jun 25, 2003 (gmt 0)

10+ Year Member



okay, grahamstewart, I tried formatting it when it was pulled from the DB and exploded it, blah blah...
the date still displayed as 12-31-1969.

When you do it are on you a Unix/Linux server?

-FM-

grahamstewart

3:21 am on Jun 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No - I'm on Windows at the moment - but that is irrelevant - I use the same code on Unix, it will work on either system.

Okay - so you pulled it out and exploded it.

So now you can print it as...


print $dateparts[0].'/'.$dateparts[1].'/'.$dateparts[2];

..or if you need to create a timestamp string from it then use strtotime as GUGU_Brasil suggested..


$birthday = strtotime( $dateparts[2].'-'.$dateparts[1].'-'.$dateparts[0] );

firemaster

10:37 pm on Jun 27, 2003 (gmt 0)

10+ Year Member



Discovered the easiest solution..
I didn't actually need the date() function, I was just using it to split them up. I then read over the exploded() function, to see how it works..
here is what I came up with.

since it is a DATE 0000-00-00 by default, I just assigned what I pulled from the db to a var:


include("db_fns.php");
$m_query = "SELECT * FROM members WHERE name = '$valid_user'";
$ms_query = mysql_query($m_query) or die (mysql_errno().": ".mysql_error()."<BR>");
while($row = mysql_fetch_array($ms_query)) {
$user_bday = $row['birthdate'];
};

Then, I exploded at the "-".

$disp_bday = explode("-", $user_bday);

so now I have my array.

$user_month = $disp_bday[1];
$user_day = $disp_bday[2];
$user_year = $disp_bday[0]

This displays from db exactly what I need and does a flawless job. Thanks for trying to help Graham, the explode() is what did it.

-FM-