0

I have stored date time in mysql from a tutorial as: 2011-02-10 01:21:39

From this, how can I extract the individual elements using php? Say I just want the year? or even just the complete date if thats not possible?

4
  • 1
    select year(date) from for year and there is mysql function to fetch everything separately Feb 10, 2011 at 17:58
  • this has been covered time and time again, almost exactly.
    – Andy
    Feb 10, 2011 at 18:01
  • @nalroff If you're not going to link to a duplicate question, then don't whine - it's not helping. :-) Feb 10, 2011 at 18:08
  • @middaparka i would, but there are like 50 of them to the right of this comment. :P
    – Andy
    Feb 10, 2011 at 19:11

9 Answers 9

4

See the strtotime() function. This will convert your date string into a timestamp. Then use date() to pull out the parts you need:

$time = strtotime('a string containing some description of the time and date');
$year = date('Y', $time);
0
4

Why not select the elements from MySQL directly?

select year(datefield), month(datefield), day(datefield) from yourtable

would return

+---------------+
| 2011 | 2 | 10 |
+---------------+

of course, if you want to do datemath in PHP, it'd be better to select the date as a UNIX_TIMESTAMP from mysql, which returns time in seconds from Jan 1/1970, which you can feed directly into PHP's date system;

SELECT UNIX_TIMESTAMP(datefield) ...

$timestamp = mysql_fetch(...)

$date = date($timestamp);
0
2

You can use the extract function of mysql from within your query like this:

SELECT EXTRACT(YEAR FROM yourDatefield) AS year,
       EXTRACT(MONTH FROM yourDatefield) AS month,
       EXTRACT(DAY FROM yourDatefield) AS day,
       EXTRACT(HOUR FROM yourDatefield) AS hour,
       EXTRACT(MINUTE FROM yourDatefield) AS minute,
       EXTRACT(SECOND FROM yourDatefield) AS second
FROM
       yourTable

Check out the extract function for more info.

2

You mean date('Y', strtotime('2011-02-10 01:21:39'));?

Go with @Marc's Solution if you are looping through MySQL result set

1

Use php's date(). You can format how ever you need. date()

2
  • 1
    This does the inverse of what David asked.
    – Tim
    Feb 10, 2011 at 17:59
  • Not really, just not necessarily a complete answer. Would require strtotime() as Alex's answer points out.
    – kmfk
    Feb 10, 2011 at 18:05
1
    <?php $original = $row['time']; 
$date = date_create($original); 
echo date_format($date, 'Y');?>

just replace the $original with your own variable. definitely check the php date() http://php.net/manual/en/function.date.php

1

Take a look at the Date and Time Functions for MySQL

For example, you can:

SELECT YEAR(DateColumn) FROM YourTable
0

date($format, strtotime($yourStringFromDB)) see PHP docs for format strings.

0
SELECT * FROM Dates
WHERE Date LIKE '%02%'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.