Question:
retrieving a date from mysql DB and splitting into 3 values in php?
john.tesco
2009-04-13 14:24:39 UTC
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
Four answers:
2009-04-13 15:03:38 UTC
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:




echo "Year: $row[usersYear], Month: $row[usersMonth], Day: $row[usersDay]";

?>
?
2009-04-13 15:41:03 UTC
When it print 1970, it means jan 1st 1970, or Unix date "0" milliseconds! So the value you pass is not a date, but 0.

If your date is stored as 1986-12-01 (standard mysql format), you can reformat it this way: (assuming that $row[0] = "1986-12-01"):



$usersYear = date ( "Y", strtotime ($row[0] );
Vladimir Kornea
2009-04-13 15:20:16 UTC
date() expects the second [optional] parameter to be a unix timestamp (integer). It doesn't understand datetime strings that MySQL returns. There are two ways to create the unix timestamp you need:



1. Use MySQL's UNIX_TIMESTAMP() function to have MySQL return a unix timestamp instead of the standard datetime string:



    SELECT UNIX_TIMESTAMP(EXAMPLE_DATE_COLUMN) AS EXAMPLE_DATE_COLUMN_UNIX_TIMESTAMP FROM EXAMPLE_TABLE



2. Use PHP's strtotime() function to convert the datetime string into a unix timestamp:



    $usersYear = date("Y", strtotime($row[0]));
?
2016-05-25 07:19:53 UTC
Maybe shes nervous or a little reserved. She just may want to take things a lot slower than you do. If you do like her and would like to see you two in a relationship, than I would respect her wanting to take it slower. Next time, don't lean in to kiss her, go right for her cheek...you may be pressuring her, specially if she is shy/nervous about the situation...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...