- Get Days in Month Using php
- How to Get the Number of Days in a Month Using date() in php
- Other Articles You’ll Also Like:
- About The Programming Expert
- Show working days of a month excluding weekends with PHP
- PHP cal_days_in_month() Function
- Definition and Usage
- Syntax
- Parameter Values
- Technical Details
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
- PHP: Calculate the Number of Working Days in a Month
Get Days in Month Using php
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.
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().
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. Check if Variable Exists in php with isset() Function
- 2. php array_flip() – Flip the Keys and Values of an Array in php
- 3. php min – Find Minimum Value of Array in php
- 4. php property_exists() – Check if Property Exists in Class or Object
- 5. Delete Cookie Using php
- 6. Creating Custom category.php With Pagination
- 7. preg_replace php – Use Regex to Search and Replace
- 8. php Delete Files with php Unlink Function
- 9. php rtrim() Function – Remove Whitespace at End of String
- 10. php str_replace – Replace String 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.
Show working days of a month excluding weekends with PHP
This tutorial will show you how to get the individual working dates of a month, whilst removing weekends, this is useful for creating charts for instance.
First create an empty array it will later store the dates.
Next I’m making use of a native function to inform php which type of calendar were working this, in this case its an Gregorian Calendar
Next set the current month and year and get the total number of days in the current month using another inbuilt function: cal_days_in_month.
$month = date('n'); // Month ID, 1 through to 12. $year = date('Y'); // Year in 4 digit 2009 format. $day_count = cal_days_in_month($type, $month, $year); // Get the amount of days
Next create a for loop to loop through the number of days of the month then format the date for each day extract the day name and check the day is not a Sun or Sat then add that date to an array called $workdays
//loop through all days for ($i = 1; $i >
From here you’ve got all the dates stored in an array to use.
To just see the array contents we can use print_r:
To use the array you could use a foreach:
foreach ($workdays as $key=>$value) < //do something here >
$workdays = array(); $type = CAL_GREGORIAN; $month = date('n'); // Month ID, 1 through to 12. $year = date('Y'); // Year in 4 digit 2009 format. $day_count = cal_days_in_month($type, $month, $year); // Get the amount of days //loop through all days for ($i = 1; $i > // look at items in the array uncomment the next line //print_r($workdays);
PHP cal_days_in_month() Function
Get the number of days in a month for a specified year and calendar:
Definition and Usage
The cal_days_in_month() function returns the number of days in a month for a specified year and calendar.
Syntax
Parameter Values
Parameter | Description |
---|---|
calendar | Required. Specifies the calendar to use. Look at the PHP Calendar Constants |
month | Required. Specifies the month in the selected calendar |
year | Required. Specifies the year in the selected calendar |
Technical Details
Return Value: | The number of days in the selected month, in the given year and calendar |
---|---|
PHP Version: | 4.1+ |
❮ PHP Calendar Reference
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
PHP: Calculate the Number of Working Days in a Month
How to calculate the number of working days in a month, in other words discount Saturdays and Sundays from a month’s total number of days.
It sounds pretty simple, but because of the shifting nature of the calendar we use, one can’t just take a guess and average it you know. No, we need a function to work out this total for each month of any particular year.
Now of course I’m sure there are for more acute, beautiful or elegant solutions to this particular problem out there, but I for one quite like the idea of seeing the logic behind what is happening step by step, which is exactly why I came up with this little function to do the work for me.
So in order to calculate the number of working days in a month by removing Saturdays and Sundays from its day count, have a look at this simple PHP function:
function calculateWorkingDaysInMonth($year = '', $month = '') < //in case no values are passed to the function, use the current month and year if ($year == '') < $year = date('Y'); >if ($month == '') < $month = date('m'); >//create a start and an end datetime value based on the input year $startdate = strtotime($year . '-' . $month . '-01'); $enddate = strtotime('+' . (date('t',$startdate) - 1). ' days',$startdate); $currentdate = $startdate; //get the total number of days in the month $return = intval((date('t',$startdate)),10); //loop through the dates, from the start date to the end date while ($currentdate $currentdate = strtotime('+1 day', $currentdate); > //end date walk loop //return the number of working days return $return; >
As you can see, the logic of the function is pretty straightforward to follow. To use the function in your code, simply call it and pass the month and year you want it to examine, meaning that