MYSQL: How to convert a string into a month (Number) [duplicate]

眉间皱痕 提交于 2019-12-04 13:44:57

To convert abbrevation to full month name use:

mysql> select monthname(str_to_date('Mar','%b'));
+------------------------------------+
| monthname(str_to_date('Mar','%b')) |
+------------------------------------+
| March                              |
+------------------------------------+

To convert abbrevation to number use:

mysql> select month(str_to_date('Mar','%b'));
+--------------------------------+
| month(str_to_date('Mar','%b')) |
+--------------------------------+
|                              3 |
+--------------------------------+

You probably need to familiarize yourself with the MySQL Date functions. For your case STR_TO_DATE() and DATE_FORMAT() would seem to be most useful. here is a link to the information:

https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date

JAN to its month number; substitute your column/value where you see FEB below:

date_format(str_to_date(concat(2012, 'FEB'), '%Y%b'), '%m')
                                     ^^^^^

1 to its short month name; substitute your column/value where indicated:

 date_format(str_to_date(concat('2012-', 1), '%Y-%m'), '%b')
                                         ^

Use %M instead of %b as the last value above to get the long month name.

javinczki

Try this one:

    $date = 'Jan';
    echo 'Month number is '.date('n', strtotime($date)).'</br>';
    echo 'and its '.date('F', strtotime($date));

Use PHP Manual - Date/Time Functions for more format options.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!