Jump to content

How to get number of days in a month and also knowing a leap year


50r

Recommended Posts

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;

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

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 by salathe
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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"

.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.