• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / Get Last Day of Month in php

Get Last Day of Month in php

April 27, 2022 Leave a Comment

To get the last day of a month, you can use the php date() function and format with “Y-m-t”. “t” returns the last day of a month.

echo date('Y-m-t');
echo date('Y-m-t', strtotime('22-04-01'));

// Output:
2022-04-30
2022-04-30

When working with date objects in php, the ability to easily change or get information about a specific time period is valuable.

One such piece of information is the last day of a given month.

We can easily get the last day of a given month with the php date() function.

To get the last date of a month, you can use the “Y-m-t” format.

Below is an example of how you can use date() to get the last date of a month in php.

echo date('Y-m-t');
echo date('Y-m-t', strtotime('22-04-01'));

// Output:
2022-04-30
2022-04-30

Getting the Number of Days in the Current Month with php

If you want to get the number of days in the current month, we can use the date() function and only pass ‘t’. As we know, ‘t’ returns the last day in a month.

Below is an example in php of how to get the number of days in the current month using date().

echo date('t');

// Output: 
30

Hopefully this article has been useful for you to learn how to get the last date of a given month in your php code.

Other Articles You'll Also Like:

  • 1.  php trim() Function – Remove Whitespace at Beginning and End of String
  • 2.  PHP random_int() Function – Generate Random Integers in PHP
  • 3.  php unset – How to Destroy Variables in php
  • 4.  Remove Last Character from String Using php
  • 5.  Check if Variable Exists in php with isset() Function
  • 6.  Get Word Count of String in php with str_word_count() Function
  • 7.  php json_encode() Function – Get JSON Representation of Variables in php
  • 8.  Truncate Strings in php Using substr()
  • 9.  php array_intersect() – Find Intersection of Two or More Arrays in php
  • 10.  php preg_match – Find Pattern in String Using Regex (Regular Expression)

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy