64

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!

Joe Doyle
  • 6,363
  • 3
  • 42
  • 45
aslum
  • 11,774
  • 16
  • 49
  • 70

17 Answers17

93

Try this:

<?php
  $date = date_parse('July');
  var_dump($date['month']);
?>
Matthew
  • 47,584
  • 11
  • 86
  • 98
78

Yes,

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

The m formats the month to its numerical representation there.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
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.

vkGunasekaran
  • 6,668
  • 7
  • 50
  • 59
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

V A S
  • 3,338
  • 4
  • 33
  • 39
5

you can also use this one:

$month = $monthname = date("M", strtotime($month));
eckes
  • 64,417
  • 29
  • 168
  • 201
lokeshpahal
  • 531
  • 4
  • 11
4
$nmonth = date("m", strtotime($month));
Kelly Copley
  • 2,990
  • 19
  • 25
  • 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 '12 at 01:57
  • @longeasy Have you passed the month along with the year, have a look at my answer below. – vkGunasekaran Mar 31 '12 at 06:21
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')
Vipin Choubey
  • 345
  • 2
  • 11
  • If you want the number of the month without leading zeroes, use 'n' (so `echo date('n', strtotime($month.' '.$year));`) – qozle Oct 13 '22 at 19:18
4
$monthname = date("F", strtotime($month));

F means full month name

Juan Mellado
  • 14,973
  • 5
  • 47
  • 54
Ranjith Siji
  • 1,125
  • 3
  • 12
  • 19
  • 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 '22 at 19:13
3
<?php
$monthNum = 5;
$monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
echo $monthName; //output: May
?>
Nikos Tsirakis
  • 709
  • 4
  • 8
  • 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 '22 at 19:17
2

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;
}
stone
  • 2,192
  • 16
  • 26
vineet
  • 13,832
  • 10
  • 56
  • 76
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);


?>
Ben Guthrie
  • 959
  • 2
  • 8
  • 17
1

Use

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

where, $month value will be 1 -> 12
PHP Ferrari
  • 15,754
  • 27
  • 83
  • 149
  • 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 '22 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

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
Haneefa
  • 51
  • 1
1

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

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
robertbasic
  • 4,325
  • 1
  • 27
  • 25
1
$dt = '2017-Jan-10';
         OR
$dt = '2017-January-10';

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

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

Mohammad Adil
  • 503
  • 6
  • 13
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']);
Mike Purcell
  • 19,847
  • 10
  • 52
  • 89
-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

Joel
  • 62
  • 1
  • 10