Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date problem in PHP and mySQL 1

Status
Not open for further replies.

adrianjohnson

Programmer
May 16, 2002
145
GB
I have two date fields in an mySQL DB - AppDate and AppAinsDate - both are declared as DATE data types in mySQL and have 2004-04-01 and 2004-05-10 as values in them, respectively.

In an attempt to retrieve these values from the db, I used two methods.
Code:

$sql = "SELECT AppID, DATE_FORMAT(AppDate, '%d %m %Y') AS ApptDate, AppAInsDate FROM table";
$appt = mysql_query($sql, $db);

$ADdy = date('d',mysql_result($appt,0,"AppAinsDate"));
$ADmn = date('m',mysql_result($appt,0,"AppAinsDate"));
$ADyr = date('Y',mysql_result($appt,0,"AppAinsDate"));




ApptDate displays correctly when output to screen. However, if I use the date function in PHP all dates are displayed as 01/01/1970 !!!

So, how can I seperate the day/month/year from the values? I'd prefer to do it in PHP, as there are alot of dates in the SELECT statement (I took them all out to shorten the space taken here) so I'd rather retreive the dates in their mySQL format, then use PHP to manipulate them. The date command shown above does not work.

Thanks,

Adrian Johnson
 
Per the PHP online manual, PHP's date() function requires as its second input not a date string, but a date integer.

strtotime() will take a date string and return a date integer, so your lines should read something like:

$ADdy = date('d',[blue]strtotime([/blue]mysql_result($appt,0,"AppAinsDate")[blue])[/blue]);

Conversely, you could just strip the string typographically:

list ($day,$month,$year) = explode (' ', mysql_result($appt,0,"AppAinsDate"));



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Hey! thanks for the tip on strtotime. saved a headache. have a star

dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top