Jump to content

[SOLVED] Converting abbreviated month (JAN) to 2 digit number (01)


jwpryor

Recommended Posts

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;

}

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.