tags:

views:

138

answers:

5

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!

+4  A: 

Yes,

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

The m formats the month to its numerical representation there.

Sarfraz
+1  A: 

Maybe use a combination with strtotime and date? http://www.php.net/manual/en/function.date.php http://www.php.net/manual/en/function.strtotime.php

robertbasic
+2  A: 

Try this:

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

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
could you please format as code?
nikic
Does that look ok?
Ben Guthrie
A: 
$nmonth = date("m", strtotime($month));
Kelly Copley
could you please format as code?
nikic