• 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 Days in Month Using php

Get Days in Month Using php

April 27, 2022 Leave a Comment

To get the days in a month, you can use the php cal_days_in_month() function.

echo cal_days_in_month(CAL_GREGORIAN,4,2022);

// Output: 
30

You can also pass ‘t’ to date() to get the number of days in the current month.

echo date('t');

// Output: 
30

When working with dates in php, the ability to easily be able to get certain pieces of information from calendars and dates is very valuable.

One such piece of information is the number of days in a month.

There are a few ways you can get the number of days in a month in php. The easiest is with the php cal_days_in_month() function.

To get the number of days in a month with cal_days_in_month(), you pass the calendar you want to use, and the month and year for which you want to find the number of days.

Below is a simple example of how to use cal_days_in_month() in php to get the number of days in a month.

echo cal_days_in_month(CAL_GREGORIAN,4,2022);

// Output: 
30

How to Get the Number of Days in a Month Using date() in php

If you want to get the number of days in the current month, we can use the date() function and pass ‘t’. ‘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

If you want to get the number of days in any month, then you can pass the timestamp of a date as the second argument to date.

echo date('t', strtotime("2022-04-01"));

// Output: 
30

Hopefully this article has helped you understand how to get the days in a month using php.

Other Articles You'll Also Like:

  • 1.  Get First Element of Array in php
  • 2.  echo php – Output One or More Strings
  • 3.  Get Current Location Latitude Longitude in php
  • 4.  Add Months to Date in php
  • 5.  Get User Agent in php with $_SERVER[‘HTTP_USER_AGENT’]
  • 6.  Get Word Count of String in php with str_word_count() Function
  • 7.  php switch case – How to Use Switch…Case Statements in php
  • 8.  Truncate Strings in php Using substr()
  • 9.  php Square Root with sqrt Function
  • 10.  How to Check If String Contains Substring in PHP

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