retrieving a date from mysql DB and splitting into 3 values in php?

  • Thread starter Thread starter john.tesco
  • Start date Start date
J

john.tesco

Guest
hi, i was trying to retrieve a date from mysql DB and split it into 3 values using a php function. its stored in a table in YYYY-MM-DD format and ive tried retrieveing it as such: $usersYear = date("Y",$row[0]);
$row[0] being the date value stored in mysql like 1986-12-01
when i tried printing $usersYear it printed a year but the incorrect year, it printed 1970 instead of 1986.. please help
that worked perfectly dhvrm, thank you very much for your help!!!
 
You can do that specifically with your SQL query.

SELECT YEAR(column) AS usersYear, MONTH(column) AS usersMonth, DAY(column) AS usersDay FROM table

(Change column to the date column's name and table to the table name; include a WHERE clause if appropriate)

Then, in PHP, when you get your recordset, you will have columns named usersYear, usersMonth and usersDay:

<?php
echo "Year: $row[usersYear], Month: $row[usersMonth], Day: $row[usersDay]";
?>
 
Back
Top