Php get the last day of the year

Last update on August 19 2022 21:50:29 (UTC/GMT +8 hours)

PHP date: Exercise-8 with Solution

Write a PHP script to get the first and last day of a month from a specified date.

Sample Solution:

PHP Code:



Sample Output:

First day : 2008-02-01 - Last day : 2008-02-29

Flowchart :

Php get the last day of the year

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP script to calculate number of days between two dates.
Next: Write a PHP script to print like : Saturday the 7th

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

PHP: Tips of the Day

PHP: PHP cURL custom headers

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'X-Apple-Tz: 0',
    'X-Apple-Store-Front: 143444,12'
));

Ref : https://bit.ly/2HgTwMp


  • Exercises: Weekly Top 16 Most Popular Topics
  • SQL Exercises, Practice, Solution - JOINS
  • SQL Exercises, Practice, Solution - SUBQUERIES
  • JavaScript basic - Exercises, Practice, Solution
  • Java Array: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : Conditional Statement
  • HR Database - SORT FILTER: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : String
  • Python Data Types: Dictionary - Exercises, Practice, Solution
  • Python Programming Puzzles - Exercises, Practice, Solution
  • C++ Array: Exercises, Practice, Solution
  • JavaScript conditional statements and loops - Exercises, Practice, Solution
  • C# Sharp Basic Algorithm: Exercises, Practice, Solution
  • Python Lambda - Exercises, Practice, Solution
  • Python Pandas DataFrame: Exercises, Practice, Solution
  • Conversion Tools
  • JavaScript: HTML Form Validation


View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a date and the task is to print the last day of the month. We will use date() and strtotime() function to get the last day of the month.

    Used PHP functions:

    • date() function: Format a local time/date.
    • strtotime() function: Parse about any English textual datetime description into a Unix timestamp.

    Examples:

    Input: 2020-04-23
    Output: Thursday
    
    Input: '2018-09-11'
    Output: Sunday

    Approach:

    • 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 employ date() method.
      Syntax:
      date( $format, $timestamp )
    • Within $format, we will pass “Y-m-t” and within $timestamp the date taken above. In terms of date “Y” gives a full numeric representation of a year in 4 digits, “m” provides numeric representation of a month and “t” gives the number of days in the given month.
    • By using “Y-m-t”, we will get the number of days in a month because of “t”, “m” and “Y” will provide the month and the year.
    • The last step is to use the date() again with “l” for format and the date achieved above as $timestamp.
    • Display the day.

    Program:

    $datestring = '2020-04-23';

    $date = strtotime($datestring);

    $lastdate = strtotime(date("Y-m-t", $date ));

    $day = date("l", $lastdate);

    echo $day;

    ?>

    get last date of month in php : Php time timestamp Get the first day and last day of the current month, last month, next month, etc.

    • get last date of month in php
      • PHP Get last day of month
      • Using the DateTime object.
      • php get start and end date of month and year
      • Last day of the month.
      • get last month php
      • Related posts

    PHP DateTime to Get the Last Date of the Month – How to get last day of a month from date in PHP ? – for example ‘last day of’, Sets the day to the last day of the current month.

    PHP: Get last day of month

    This month.
    The last day of this month:

    From a given date.

    Using the DateTime object.

    format('Y-m-t');
    

    php get start and end date of month and year

    First day of the month.

    echo date('Y-m-01', strtotime($user_first_dt));
    

    Last day of the month.

    echo date('Y-m-t', strtotime($user_first_dt));
    

    Don’t Miss : Current Month In Php

    get last month php

    get last month php

    $currentMonth = date('M');// if number, then date('m');
    //Last month
    $lastMonth = Date("F", strtotime("first day of previous month");
    var_dump($lastMonth);
    
    $nextMonth = Date("F", strtotime("first day of next month");
    var_dump($nextMonth);
    

    get last month using php

    $lastMonth = Date("F", strtotime("first day of previous month");
    var_dump($lastMonth);
    
    $nextMonth = Date("F", strtotime("first day of next month");
    var_dump($nextMonth);
    

    I hope you get an idea about get last date of month in php.
    I would like to have feedback on my infinityknow.com blog.
    Your valuable feedback, question, or comments about this article are always welcome.
    If you enjoyed and liked this post, don’t forget to share.

    I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

    How to find the last day of the month in php?

    $date = strtotime ( $datestring ); // Last date of current month. $day = date ( "l" , $lastdate );

    How to get first and last date of year in php?

    Basically date('Y') returns the value of current year. and the first day of each year starts like 2000-01-01. So this will help you to get exact output. Basically date('Y') returns the value of current year.

    How can I get first and last date of month in php?

    php echo 'First Date = ' . date('Y-m-01') . '
    '; echo 'Last Date = ' . date('Y-m-t') .
    .
    Y gives you the 4-digit year from the timestamp ('2010').
    m gives you the numeric month from the timestamp, with a leading zero ('04').
    t gives you the number of days in the timestamp's month ('30').

    How do I get this year in php?

    To get the current year using PHP's date function, you can pass in the “Y” format character like so: //Getting the current year using //PHP's date function. $year = date("Y"); echo $year; The example above will print out the full 4-digit representation of the current year.