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;

}

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.