Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

extract year, month and date from mysql date field

Posted on 2005-04-09
5
Medium Priority
?
13,384 Views
Last Modified: 2013-12-12
Hi, I've got a mysql field called birthday of type date : It seems to display in the form YYYY-MM-DD. Now I want to extract the year, month and date and store them to seperate variables

//get from database and store in $birthday
$birthday=$row["birthday"];

$date = getdate ($birthday);
$year=$date['year'];

Now for some reason $year always displays 1970, although $birthday shows the proper date.
Thanks,
0
Comment
Question by:skylabel
  • 3
5 Comments
 
LVL 4

Assisted Solution

by:cachedVB
cachedVB earned 480 total points
ID: 13744252
Hi skylabel,

Try using this instead:
list($year, $month, $day) = split('-', $date);
echo "Month: $month; Day: $day; Year: $year<br />\n";

cachedVB
0
 
LVL 32

Accepted Solution

by:
ldbkutty earned 640 total points
ID: 13744261
$res = mysql_query("SELECT EXTRACT(YEAR FROM birthday) AS birthyear, EXTRACT(MONTH FROM birthday) AS birthmonth, EXTRACT(DAY FROM birthday) AS birthdate FROM tablename") or die("Sql error: " . mysql_error());

while($row = mysql_fetch_array($res)) {
 echo $row["birthyear"] . "<br/>";
 echo $row["birthmonth"] . "<br/>";
 echo $row["birthdate"] . "<br/>";
}
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13744276
getdate retrives the date information of the TIMESTAMP, it won't consider for DATE/DATETIME formats.

This is another option:

$birthday = $row["birthday"];
echo date("Y", $birthday);
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13744285
Or this:

$date = getdate(strtotime($row["birthday"]));
$year = $date['year'];
0
 
LVL 9

Assisted Solution

by:gtkfreak
gtkfreak earned 480 total points
ID: 13744424
You could also use the following SQL to get the dayofmonth, month and year
select dayofmonth(yourdate) as dday, month(yourdate) as dmonth, year(yourdate) as dyear, ...........
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question