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 Link to comment https://forums.phpfreaks.com/topic/60776-solved-converting-abbreviated-month-jan-to-2-digit-number-01/ 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; } Link to comment https://forums.phpfreaks.com/topic/60776-solved-converting-abbreviated-month-jan-to-2-digit-number-01/#findComment-302333 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 Link to comment https://forums.phpfreaks.com/topic/60776-solved-converting-abbreviated-month-jan-to-2-digit-number-01/#findComment-302338 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! Link to comment https://forums.phpfreaks.com/topic/60776-solved-converting-abbreviated-month-jan-to-2-digit-number-01/#findComment-302340 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 Link to comment https://forums.phpfreaks.com/topic/60776-solved-converting-abbreviated-month-jan-to-2-digit-number-01/#findComment-302622 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. Link to comment https://forums.phpfreaks.com/topic/60776-solved-converting-abbreviated-month-jan-to-2-digit-number-01/#findComment-302641 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.