View Full Version : [PHP] Month name -> number
NeoDreamer
July 3rd, 2006, 05:21 PM
Is there any way to convert a string containing the month name into the month number? For example, if $name = "Jan", I want "1".
Jeff Wheeler
July 3rd, 2006, 05:25 PM
Are you using PHP5?
ramie
July 3rd, 2006, 05:29 PM
have a look at using a switch statement http://uk.php.net/switch
Jeff Wheeler
July 3rd, 2006, 05:31 PM
Yeah, I saw gonna suggest a switch if you don't have PHP5. If you do, though, just use the strptime function.
ramie
July 3rd, 2006, 05:37 PM
thats not a true string conversation tho is it? my php is intentionally rusty which is why I ask, that could be quite useful. So if you pass strptime a string like feb it will return 2?
bigmtnskier
July 3rd, 2006, 05:39 PM
Or you could just use an array.
$map = array(
'jan' => 1,
'feb' => 2,
'mar' => 3,
'apr' => 4,
'may' => 5,
'jun' => 6,
'jul' => 7,
'aug' => 8,
'sept' => 9,
'oct' => 10,
'nov' => 11,
'dec' => 12
);
$monthnumber = $map[strtolower($name)];
:)
Jeff Wheeler
July 3rd, 2006, 05:45 PM
Or, with PHP5:
<?php
$time = strptime('March', '%B');
echo $time['tm_mon']+1;
?>
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.