Angel
Angel

Reputation: 1009

Get the date if the given is month php

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?

Upvotes: 0

Views: 1495

Answers (5)

Shan
Shan

Reputation: 1103

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))}}

Upvotes: 1

Nishad Up
Nishad Up

Reputation: 3615

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

Upvotes: 1

Sayed Mohd Ali
Sayed Mohd Ali

Reputation: 2254

$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.

Upvotes: 1

Omi
Omi

Reputation: 4007

Use Carbon

{{ Carbon\Carbon::createFromDate(2019,$company_details->accounting_period_month,01)->format('M') }}

Upvotes: 1

Er Pkumar soni
Er Pkumar soni

Reputation: 320

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];
?>

Upvotes: -2

Related Questions