convert month from name to number

PhpTime

Php Problem Overview


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!

Php Solutions


Solution 1 - Php

Try this:

<?php
  $date = date_parse('July');
  var_dump($date['month']);
?>

Solution 2 - Php

Yes,

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

The m formats the month to its numerical representation there.

Solution 3 - Php

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.

Solution 4 - Php

$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

Solution 5 - Php

you can also use this one:

$month = $monthname = date("M", strtotime($month));

Solution 6 - Php

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

Solution 7 - Php

$monthname = date("F", strtotime($month));

F means full month name

Solution 8 - Php

<?php
$monthNum = 5;
$monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
echo $monthName; //output: May
?>

Solution 9 - Php

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')

Solution 10 - Php

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);


?>

Solution 11 - Php

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;
}

Solution 12 - Php

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

Solution 13 - Php

Use

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

where, $month value will be 1 -> 12

Solution 14 - Php

$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

Solution 15 - Php

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

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

Solution 16 - Php

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

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

Solution 17 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaslumView Question on Stackoverflow
Solution 1 - PhpMatthewView Answer on Stackoverflow
Solution 2 - PhpSarfrazView Answer on Stackoverflow
Solution 3 - PhpvkGunasekaranView Answer on Stackoverflow
Solution 4 - PhpVineesh KalarickalView Answer on Stackoverflow
Solution 5 - PhplokeshpahalView Answer on Stackoverflow
Solution 6 - PhpKelly CopleyView Answer on Stackoverflow
Solution 7 - PhpRanjith SijiView Answer on Stackoverflow
Solution 8 - PhpNikos TsirakisView Answer on Stackoverflow
Solution 9 - PhpVipin ChoubeyView Answer on Stackoverflow
Solution 10 - PhpBen GuthrieView Answer on Stackoverflow
Solution 11 - PhpvineetView Answer on Stackoverflow
Solution 12 - PhprobertbasicView Answer on Stackoverflow
Solution 13 - PhpPHP FerrariView Answer on Stackoverflow
Solution 14 - PhpHaneefaView Answer on Stackoverflow
Solution 15 - PhpMohammad AdilView Answer on Stackoverflow
Solution 16 - PhpMike PurcellView Answer on Stackoverflow
Solution 17 - PhpJoelView Answer on Stackoverflow