简体   繁体   中英

convert month from name to number

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!

Try this:

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

Yes,

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

The m formats the month to its numerical representation there.

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.

$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

你也可以使用这个:

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

F表示完整的月份名称

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

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

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


?>

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

Use

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

where, $month value will be 1 -> 12
$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

也许结合使用strtotime()date()

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

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

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

使用 PHP 5.4,您可以将 Matthew 的答案变成单行式:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM