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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • 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.  preg_replace php – Use Regex to Search and Replace
  • 2.  php array_diff() – Find Elements of Array Not Present in Other Arrays
  • 3.  Get Current Month of Date in php
  • 4.  How to Reverse Array in php Without a Function or Other Variables
  • 5.  php tanh – Find Hyperbolic Tangent of Number Using tanh() Function
  • 6.  php sleep() Function – Pause Execution of Code in php
  • 7.  php array_pop() – Remove Last Element from Array
  • 8.  How to Get Current System IP Address in php
  • 9.  php preg_match – Find Pattern in String Using Regex (Regular Expression)
  • 10.  php asinh – Find Hyperbolic Arcsine of Number Using php asinh() Function

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