50r Posted January 5, 2013 Share Posted January 5, 2013 I am working on a tenance script that will need to know how many days are in the current month and also to check if the current february is not in a leap year. this is the code that i was trying to use but is there any better way to handle this? $month = strftime('%m'); $year = strftime('%Y'); if ($month == 1 || $month == 3 || $month == 5 || $month == 7 || $month == 8 || $month == 10 || $month == 12) { $days = 31; } elseif ($month == 4 || $month == 6 || $month == 9 || $month == 11) { $days = 30; } elseif ($month == 2) { if(($year % 4 == 0) && ($year % 100 != 0) || ($year %400 == 0)) { $days = 28; } else { $days = 29; } } echo $days; Quote Link to comment https://forums.phpfreaks.com/topic/272727-how-to-get-number-of-days-in-a-month-and-also-knowing-a-leap-year/ Share on other sites More sharing options...
scootstah Posted January 5, 2013 Share Posted January 5, 2013 Perhaps cal_days_in_month is what you're looking for. Quote Link to comment https://forums.phpfreaks.com/topic/272727-how-to-get-number-of-days-in-a-month-and-also-knowing-a-leap-year/#findComment-1403426 Share on other sites More sharing options...
50r Posted January 5, 2013 Author Share Posted January 5, 2013 Thanks for the quick replay i also found this one which also works perfectly fine. Â function get_days_in_month($month, $year) { return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year %400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31); } Quote Link to comment https://forums.phpfreaks.com/topic/272727-how-to-get-number-of-days-in-a-month-and-also-knowing-a-leap-year/#findComment-1403433 Share on other sites More sharing options...
jcbones Posted January 5, 2013 Share Posted January 5, 2013 I would suggest the DateTime Class. Quote Link to comment https://forums.phpfreaks.com/topic/272727-how-to-get-number-of-days-in-a-month-and-also-knowing-a-leap-year/#findComment-1403438 Share on other sites More sharing options...
salathe Posted January 6, 2013 Share Posted January 6, 2013 (edited) As jcbones suggests, the DateTime class is your friend. Â Â // OOP style $date = new DateTime; Â Â // uses current date and time by default echo $date->format('t'); // prints number of days in the month echo $date->format('L'); // prints 1 if leap year, 0 otherwise // Procedural style $date = date_create(); echo date_format($date, 't'); echo date_format($date, 'L'); Â See the date() manual page for the meanings of the letters used with format(). Edited January 6, 2013 by salathe Quote Link to comment https://forums.phpfreaks.com/topic/272727-how-to-get-number-of-days-in-a-month-and-also-knowing-a-leap-year/#findComment-1403615 Share on other sites More sharing options...
DavidAM Posted January 7, 2013 Share Posted January 7, 2013 $days = date('d', mktime(0, 0, 0, $month + 1, 1, $year) - 1) Quote Link to comment https://forums.phpfreaks.com/topic/272727-how-to-get-number-of-days-in-a-month-and-also-knowing-a-leap-year/#findComment-1403791 Share on other sites More sharing options...
salathe Posted January 7, 2013 Share Posted January 7, 2013 $days = date('d', mktime(0, 0, 0, $month + 1, 1, $year) - 1) Â Why would anyone choose to do that? There are several "huh, wha, why?" points in that single line of code. Quote Link to comment https://forums.phpfreaks.com/topic/272727-how-to-get-number-of-days-in-a-month-and-also-knowing-a-leap-year/#findComment-1403796 Share on other sites More sharing options...
Barand Posted January 7, 2013 Share Posted January 7, 2013 simpler, if you don't use datetime object, is  $days = date('t', mktime(0,0,0,$month,1,$year)); Quote Link to comment https://forums.phpfreaks.com/topic/272727-how-to-get-number-of-days-in-a-month-and-also-knowing-a-leap-year/#findComment-1403798 Share on other sites More sharing options...
haku Posted January 7, 2013 Share Posted January 7, 2013 I'm still not sure what the OP was asking. Do they want to know how many days there are in the current month, or do they want to know whether or not the current year is a leap year? And if they want to know both, then how do these two things relate to each other? Quote Link to comment https://forums.phpfreaks.com/topic/272727-how-to-get-number-of-days-in-a-month-and-also-knowing-a-leap-year/#findComment-1403815 Share on other sites More sharing options...
scootstah Posted January 7, 2013 Share Posted January 7, 2013 I'm still not sure what the OP was asking. Do they want to know how many days there are in the current month, or do they want to know whether or not the current year is a leap year? And if they want to know both, then how do these two things relate to each other? Â I believe he only wanted to know about leap year so that he could determine if February had 28 or 29 days. However, several PHP functions already handle that logic, so it's sort of irrelevant. Quote Link to comment https://forums.phpfreaks.com/topic/272727-how-to-get-number-of-days-in-a-month-and-also-knowing-a-leap-year/#findComment-1403844 Share on other sites More sharing options...
50r Posted January 8, 2013 Author Share Posted January 8, 2013 I believe he only wanted to know about leap year so that he could determine if February had 28 or 29 days. However, several PHP functions already handle that logic, so it's sort of irrelevant. Â what i was trying to ask was, i am working on a system that will manage tenants so tenant on their registration, need to pick a subscription cycle which can be monthly,quarterly semi-annuall, and annualy. Â what i did not know is that there were such simple functions like mktime and the date(t) format. But my intent in this question was the ability to add up a three or six or monthly cycle regardles of the numbers in the month but just bind it all in just a simple variable monthy, quarterly and soo on. Â thanks to you guy good bless. "RIP Steve jobs" . Quote Link to comment https://forums.phpfreaks.com/topic/272727-how-to-get-number-of-days-in-a-month-and-also-knowing-a-leap-year/#findComment-1404123 Share on other sites More sharing options...
Christian F. Posted January 8, 2013 Share Posted January 8, 2013 For something like that PHP has a really nifty thing called "relative date formats", which makes it extremely easy to add any amount of interval to a time/date. They can be used with either the DateTime class, or the strototime () function. I recommend the former, as it's more future proof. Quote Link to comment https://forums.phpfreaks.com/topic/272727-how-to-get-number-of-days-in-a-month-and-also-knowing-a-leap-year/#findComment-1404136 Share on other sites More sharing options...
50r Posted January 9, 2013 Author Share Posted January 9, 2013 thanks chris Quote Link to comment https://forums.phpfreaks.com/topic/272727-how-to-get-number-of-days-in-a-month-and-also-knowing-a-leap-year/#findComment-1404402 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.