Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert month from name to number

Tags:

php

time

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!

like image 511
aslum Avatar asked Jul 19 '10 17:07

aslum


People also ask

How do I convert month name to number in Excel?

An alternative way to get a month number from an Excel date is using the TEXT function: =TEXT(A2, "m") - returns a month number without a leading zero, as 1 - 12. =TEXT(A2,"mm") - returns a month number with a leading zero, as 01 - 12.

How do I convert month name to month number?

Select the cells containing the month names that you want to convert. Press CTRL+1 from your keyboard or right-click and select “Format Cells” from the context menu that appears. This will open the Format Cells dialog box. Click on the Number tab and select “Custom” from the list under Category.

How do I convert text to months in Excel?

Using the & symbol joins the 1 to the first three characters of the cell or 1Sep. Excel recognises that as a date format and treats it like a date for the MONTH function to then extract the month number. Because if you type 1September it also returns a date.


2 Answers

Try this:

<?php   $date = date_parse('July');   var_dump($date['month']); ?> 
like image 188
Matthew Avatar answered Oct 16 '22 07:10

Matthew


Yes,

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

The m formats the month to its numerical representation there.

like image 26
Sarfraz Avatar answered Oct 16 '22 05:10

Sarfraz