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']);
?>
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);
?>
$nmonth = date("m", strtotime($month));
$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
<?php
$monthNum = 5;
$monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
echo $monthName; //output: May
?>
you can also use this one:
$month = $monthname = date("M", strtotime($month));
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')
With PHP 5.4, you can turn Matthew's answer into a one-liner:
$date = sprintf('%d-%d-01', $year, date_parse('may')['month']);
$dt = '2017-Jan-10';
OR
$dt = '2017-January-10';
echo date('Y-m-d', strtotime($dt));
echo date('Y/m/d', strtotime($dt));
Maybe use a combination with strtotime()
and date()
?
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
$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
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.
Use
date("F", mktime(0, 0, 0, ($month)));
where, $month value will be 1 -> 12
$monthname = date("F", strtotime($month));
F
means full month name
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;
}
Source: Stackoverflow.com