7

In my MySQL server, I have rows that have the date of my created content.
Example: 2013-03-17 02:53:47

For now, I am using:

$date = $fetch[thedaterownamegoeshere];

<?php echo $date; ?>

But instead of getting the full content of the date (2013-03-17 02:53:47), I'd like to get only the month – in this example, I wish to have the number "03" as the result. How can I do that?

10

SUGGESTION #1

You could use the MONTH() function

mysql> select MONTH(NOW());
+--------------+
| MONTH(NOW()) |
+--------------+
|            3 |
+--------------+
1 row in set (0.03 sec)

mysql> select MONTH('2013-03-17 02:53:47');
+------------------------------+
| MONTH('2013-03-17 02:53:47') |
+------------------------------+
|                            3 |
+------------------------------+
1 row in set (0.04 sec)

mysql>

You would have to put the MONTH function into the SQL command you are running.

SUGGESTION #2

You could also try the PHP Date Function

$month = date("m",$date)

Also DATE_FORMAT(date_column, '%c') to get '3' as result and DATE_FORMAT(date_column, '%m') to get '03' as result.

1
  • 1
    Be careful of w3schools references... I got severely punished by some nazi downvoters for using them as a reference, even though the specific reference was 100% correct. Apparently specific correctness does not matter in light of using a resource with a bad reputation (which is ridiculous if you ask me - plenty of correct examples can be found on slack sites, should these then not be referenced?) Guilt by association. – Stefan Oct 3 '19 at 10:28
0

This will work for you.

 $date = date('M', strtotime($fetch['thedaterownamegoeshere']));
1
  • Why do they need strtotime()? – ypercubeᵀᴹ Dec 30 '17 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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