0

I don't want to use the datepicker because I dont need the year so I create 2 select list where my first select will be the month and the second is the days.

Everything is okay, it will save in my db but here's my problem. I want to display the full name of the month instead of number.

Example:

Accounting Period

(Month list)   (Day list)
 0-12             1-31

Scenario: I choose oct as my month and 5 is my day.

value="10" Oct        value="5"   5

View

Select Month List
    <select name="accounting_period_month">'+
      '<option value="01">January</option>'+
      '<option value="02">February</option>'+
      '<option value="03">March</option>'+
      '<option value="04">April</option>'+
      '<option value="05">May</option>'+
      '<option value="06">June</option>'+
      '<option value="07">July</option>'+
      '<option value="08">August</option>'+
      '<option value="09">September</option>'+
      '<option value="10">October</option>'+
      '<option value="11">November</option>'+
      '<option value="12">December</option>'+
    '</select>

It will be save as 10 and 5.

View in displaying the month list

{{ date('M',strtotime($company_details->accounting_period_month))}}

NOTE: Don't mind the ' + because I append it in my jquery.

Now my question is how can I display the word Oct instead of 10?

Angel
  • 966
  • 4
  • 25
  • 60

5 Answers5

1
$monthNum  = 3;
$dateObj   = DateTime::createFromFormat('!m', $monthNum);
$monthName = $dateObj->format('F'); // March

for an older version of PHP

$monthNum  = 3;
$monthName = date('F', mktime(0, 0, 0, $monthNum, 10)); // March

If you want the just 3 letter month name example Mar, you can change F to M. The list of all available formatting options can be found in the PHP manual documentation.

Sayed Mohd Ali
  • 2,156
  • 3
  • 12
  • 28
1

Use Carbon

{{ Carbon\Carbon::createFromDate(2019,$company_details->accounting_period_month,01)->format('M') }}
Omi
  • 3,954
  • 5
  • 21
  • 41
1

Here is multiple way to print month from given number.

1. date() function along with parameter 'F'

Code example:

$month_num = 10;
echo date("F", mktime(0, 0, 0, $month_num, 10)); //output: October

2. By creating php date object using createFromFormat()

Code Example

$dateObj   = DateTime::createFromFormat('!m', $monthNum);
echo "month name: ".$dateObj->format('F'); // Output: October

3. strtotime() function

echo date("F", strtotime('00-'.$monthNum.'-01')); // Output: October

4. mktime() function

echo date("F", mktime(null, null, null, $monthNum)); // Output: October

5. By using jdmonthname()

$jd=gregoriantojd($monthNum,10,2019);
echo jdmonthname($jd,0); // Output: Oct
Nishad Up
  • 3,457
  • 1
  • 28
  • 32
1

Since you have Laravel Tag in your question :

$data = YourModel::select('*')
    ->addSelect(DB::raw('MONTHNAME(accounting_period_month) AS month_name')
    ->get();

print_r($data);

See @Karol Samborski comment,

i dont understand whats the issue with the below statement

{{ date('M',strtotime($company_details->accounting_period_month))}}
Shan
  • 1,081
  • 1
  • 12
  • 35
-2

try this code.

Create a array and define the all month.

Here code for javascript.

<script type="text/javascript">
    var i = 10;
    var month = ["jan", "feb", "march", "april", "may", "jun", "july","aug", 
                 "sep","oct","nov","dec"];
    console.log(month[i-1])

</script>

Here code for Php

<?php 
   $i = 10;
   $month = array("jan", "feb", "march", "april", "may", "jun", 
                   "july","aug", "sep","oct","nov","dec");
   echo $month[$i-1];
?>
Er Pkumar soni
  • 152
  • 1
  • 7
  • I used jquery/javascript ,this part will be append after I clicked the edit button. – Angel Feb 21 '19 at 06:01
  • but you will use PHP first when you retrieve data from the database. at that time only you can easily convert into month name using PHP. there will no need for javascript or JQuery... – Sayed Mohd Ali Feb 21 '19 at 06:03
  • but here u can customize the name of month – Er Pkumar soni Feb 21 '19 at 06:08
  • It is better to use code which is already available than creating your own for it... This is known as code reusability... – Sayed Mohd Ali Feb 21 '19 at 06:15
  • 1
    example consider C program: you have one method called factorial() with 10 or 20 lines of code. when ever you want to calculate factorial you no need to write the function again you can just call with the function there you used the code this is called code re-usability. – Sayed Mohd Ali Feb 21 '19 at 06:15