Get the date if the given is month php

get number of days in specific month php
get all days in month php
php date
php get month from date
php get all saturdays in a month
strtotime
get last day of month php
php get current date

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?

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

PHP Date Exercise: Get the first and last day of a month from a , Write a PHP script to get the first and last day of a month from a specified date. Sample Solution: PHP Code: <?php $dt = "2008-02-23"; echo 'First� Version Description; 5.1.1: There are useful constants of standard date/time formats that can be used to specify the format parameter.: 5.1.0: The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT.

Use Carbon

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

cal_days_in_month - Manual, cal_days_in_month — Return the number of days in a month for a given year Remember if you just want the days in the current month, use the date function: Get a Date. The required format parameter of the date() function specifies how to format the date (or time). Here are some characters that are commonly used for dates: d - Represents the day of the month (01 to 31) m - Represents a month (01 to 12) Y - Represents a year (in four digits) l (lowercase 'L') - Represents the day of the week

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

Date/Time Functions - Manual, The message below will appear if the current date is between the start and endDate - used I needed a function that determined the last Sunday of the month. t - The number of days in the given month; L - Whether it's a leap year (1 if it is a leap year, 0 otherwise) DATE_RFC3339 - Same as DATE_ATOM (since PHP 5.1.3

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

DateTime::modify - Manual, A date/time string. Valid formats are explained in Date and Time Formats. 3 years ago. a slightly more compact way of getting the month shift <?php /** In the code above, we simply provide PHP’s date function with the format characters “Y-m-t”. If you test it out for yourself, you’ll see that that a YYYY-MM-DD date string is returned and that it looks something like this: “2016-08-31” From a given date. The last day of a month – from a given date:

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

strtotime - Manual, The function expects to be given a string containing an English date format and will try to If it's a leap year, you'll get March on the 30th or 31st of the month. Here's a one-line function I just wrote to find the numbers of days in a month that doesn't depend on any other functions. The reason I made this is because I just found out I forgot to compile PHP with support for calendars, and a class I'm writing for my website's open source section was broken. So rather than recompiling PHP (which I will get around to tomorrow I guess), I just wrote this

PHP date() Function, Format a local date and time and return the formatted date strings: d - The day of the month (from 01 to 31); D - A textual representation of a day (three letters); j - The day of t - The number of days in the given month; L - Whether it's a leap year (1 if it is a leap year, 0 otherwise) Default is the current local time (time())� PHP: Get the first Monday of a month (and more) This is a small cheat sheet for PHP’s strtotime function, which can be used to convert textual sentences such as “next Friday” and “last Monday” into UNIX timestamps and formatted dates.

PHP cal_days_in_month() Function, Get the number of days in a month for a specified year and calendar: <?php $d= cal_days_in_month(CAL_GREGORIAN,10,2005); echo "There was $d days in� Method 3: Using DateTime class to get current month. From PHP 5.2, PHP provides some ready-made classes to help developers to solve daily problems they face. One of those classes is DateTime class which solves date time related issues. To get current month using DateTime class, follow the two steps-Create an object of DateTime() class.

How to get last day of a month from date in PHP ?, Given a date stored in a variable as a string, the step is to convert it into date format using strtotime() function. Once we get the date, we will� It is very easy to do by using built-in functions like strtotime(), date_add() in PHP. Method 1: Using strtotime() Function: The strtotime() Function is used to convert an English textual date-time description to a UNIX timestamp.

Comments
  • The line {{ date('M',strtotime($company_details->accounting_period_month))}} should give you Oct not 10 so what is the problem with that?
  • Possible duplicate of Convert number to month name in PHP
  • Oct is my select list.
  • why do you need to pass 10 at the first place if all you want back is oct?... you can handle it in backend if you have to pass 10 as a number for month
  • create a month array define keys as month number and values as month value
  • May I know what is 10?
  • here is the documentation
  • mktime(hour,minute,second,month,day,year,is_dst);
  • as I previously suggested that you can create the array but this is predefined functions of PHP you can directly get month name just by passing month number...
  • I used jquery/javascript ,this part will be append after I clicked the edit button.
  • 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...
  • but here u can customize the name of month
  • It is better to use code which is already available than creating your own for it... This is known as code reusability...
  • 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.