66

Is there an easy way to change $month = "July"; so that $nmonth = 7 (07 would be fine too). I could do a case statement, but surely there is already a function to convert? EDIT: I wish I could accept multiple answers, cause two of you basically gave me what I needed by your powers combined.

$nmonth = date('m',strtotime($month));

That will give the numerical value for $month. Thanks!

17 Answers 17

94

Try this:

<?php
  $date = date_parse('July');
  var_dump($date['month']);
?>
1
  • this should be #1.... simple one-liner Mar 24 at 1:47
78

Yes,

$date = 'July 25 2010';
echo date('d/m/Y', strtotime($date));

The m formats the month to its numerical representation there.

0
31

An interesting look here, the code given by kelly works well,

$nmonth = date("m", strtotime($month));

but for the month of february, it won't work as expected when the current day is 30 or 31 on leap year and 29,30,31 on non-leap year.It will return 3 as month number. Ex:

$nmonth = date("m", strtotime("february"));

The solution is, add the year with the month like this:

$nmonth = date("m", strtotime("february-2012"));

I got this from this comment in php manual.

1
  • amazing....! Never thought about leap trouble years...
    – gumuruh
    Feb 10, 2022 at 7:09
13
$string = "July";
echo $month_number = date("n",strtotime($string));

returns '7' [month number]

Use date("m",strtotime($string)); for the output "08"

For more formats reffer this..
http://php.net/manual/en/function.date.php

5

you can also use this one:

$month = $monthname = date("M", strtotime($month));
4
$nmonth = date("m", strtotime($month));
2
  • I tested this solution and in some cases it returns the wrong number, e.g. '5' for 'April'. Anybody got an idea why? I'll have a closer look at it later.
    – Patrick
    Mar 31, 2012 at 1:57
  • @longeasy Have you passed the month along with the year, have a look at my answer below. Mar 31, 2012 at 6:21
4
$monthname = date("F", strtotime($month));

F means full month name

1
  • This returns the full month name, whereas the OP is looking for the month number. Replacing the "F" with "m" (for two digit month number i.e. '05' for May) or "n" (for single digit month number i.e. "5" for May) yields the result that OP is looking for.
    – qozle
    Oct 13, 2022 at 19:13
4

If you want number of month from string name then

$month = 'August';
$year = 2019;
echo date('m',strtotime($month.' '.$year));

Gives 08

Or If you want the Full name of the month then

echo date('F')

OR if you want the half name of the month then

echo date('M')
1
  • If you want the number of the month without leading zeroes, use 'n' (so echo date('n', strtotime($month.' '.$year));)
    – qozle
    Oct 13, 2022 at 19:18
3
<?php
$monthNum = 5;
$monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
echo $monthName; //output: May
?>
1
  • OP is looking for the number of the month, not the month name. They're trying to translate from month name to month number. "F" in date() returns the month name...use 'm' or 'n' for the month number with or without leading zeroes (respectively)
    – qozle
    Oct 13, 2022 at 19:17
2

It may be easiest to create a fake date so you can use the date function.

Excellent reference here: http://php.net/manual/en/function.date.php

Example:

<?
$month = 7;

$tempDate = mktime(0, 0, 0, $month, 1, 1900); 

echo date("m",$tempDate);


?>
0
1

Maybe use a combination with strtotime() and date()?

1
  • A complete working example here as a solution would be helpful.
    – qozle
    Oct 13, 2022 at 19:20
1

Use

date("F", mktime(0, 0, 0, ($month)));

where, $month value will be 1 -> 12
1
  • OP wanted the number of the month, not the name. Use "m" or "n" in place of "F" (in date("F", ...) to get the month number with or without leader zeroes (respectively).
    – qozle
    Oct 13, 2022 at 19:20
1
$date = 'Dec 25 2099';
echo date('d/m/Y', strtotime($date));

This returns 01/01/1970, that means php doesn't support all dates, it returns correct formatted date till Jan 19 2038 but Jan 20 2038 returns 01/01/1970

1
$dt = '2017-Jan-10';
         OR
$dt = '2017-January-10';

echo date('Y-m-d', strtotime($dt));

echo date('Y/m/d', strtotime($dt));

1

Above answer is good. Here is another way to do:-

function getMonthNumber($monthStr) {
//e.g, $month='Jan' or 'January' or 'JAN' or 'JANUARY' or 'january' or 'jan'
$m = ucfirst(strtolower(trim($monthStr)));
switch ($m) {
    case "January":        
    case "Jan":
        $m = "01";
        break;
    case "February":
    case "Feb":
        $m = "02";
        break;
    case "March":
    case "Mar":
        $m = "03";
        break;
    case "April":
    case "Apr":
        $m = "04";
        break;
    case "May":
        $m = "05";
        break;
    case "June":
    case "Jun":
        $m = "06";
        break;
    case "July":        
    case "Jul":
        $m = "07";
        break;
    case "August":
    case "Aug":
        $m = "08";
        break;
    case "September":
    case "Sep":
        $m = "09";
        break;
    case "October":
    case "Oct":
        $m = "10";
        break;
    case "November":
    case "Nov":
        $m = "11";
        break;
    case "December":
    case "Dec":
        $m = "12";
        break;
    default:
        $m = false;
        break;
}
return $m;
}
2
  • 1
    February, not "Febuary" Mar 5, 2018 at 7:54
  • PHP built-in functions should be preferred, e.g. print((DateTimeImmutable::createFromFormat('j-M-Y', '15-Jul-2009'))->format('m'));
    – Jaleks
    Oct 9 at 12:08
0

With PHP 5.4, you can turn Matthew's answer into a one-liner:

$date = sprintf('%d-%d-01', $year, date_parse('may')['month']);
-2

I know this might seem a simple solution, but why not just use something like this

<select name="month">
  <option value="01">January</option>
  <option value="02">February</option>
  <option selected value="03">March</option>
</select>

The user sees February, but 02 is posted to the database

Your Answer

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

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