jwpryor Posted July 19, 2007 Share Posted July 19, 2007 I need to convert an abbreviated uppercase 3 letter month (i.e. JAN) to its corresponding 2 digit number (i.e. 01). How would I do this in php/SQL? The month is input by the user and is not the currnet month. Thank you, Julia Quote Link to comment Share on other sites More sharing options...
dbo Posted July 19, 2007 Share Posted July 19, 2007 well it's not pretty... but you could just do: function convert_month($month) { $my_month = ""; if( $month == "JAN" ) { $my_month = "01"; } else if( $month == "FEB" ) { $my_month = "02"; } ....... return $my_month; } Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 19, 2007 Share Posted July 19, 2007 The strtotime() function is amazingly robust. Although it doesn't like it if you just put in JAN or FEB ect, its fine if you add a year after. Doesn't really matter what the year is, but might as well do the current year: <?php $year = date('Y'); $timestamp = strtotime('JAN'.$year); $month = date('m',$timestamp); echo $month; ?> produces 01 Quote Link to comment Share on other sites More sharing options...
dbo Posted July 19, 2007 Share Posted July 19, 2007 I didn't try your code but I like it better! Quote Link to comment Share on other sites More sharing options...
jwpryor Posted July 19, 2007 Author Share Posted July 19, 2007 Thank you very much - the strtotime() and date() were exactly what I needed. Julia Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 19, 2007 Share Posted July 19, 2007 If you are storing dates/times in mysql its always considered the best method to use the UNIX timestamp (its a value in seconds from the epoch) this number contains the most information in the smallest package and you can search for it using the php date functions using the "U" as your desired time return. You can retrive Year,Month,Day,Hour,Minuites,Seconds all from a single number instead of wasting more mysql fields than are needed. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.