开发者

How do I calculate the number of seconds in a month in PHP?

开发者 https://www.devze.com 2023-01-25 23:39 出处:网络
How do I accurately determine the numb开发者_如何学运维er of seconds in a month using PHP? Is the best way to take the number of seconds in a year and divide by 12?Multiply the number of days in the m

How do I accurately determine the numb开发者_如何学运维er of seconds in a month using PHP? Is the best way to take the number of seconds in a year and divide by 12?


Multiply the number of days in the month by 60 * 60 * 24.


Due to daylights savings... take a good datetime library in your language and calculate the difference between the first day of the month 0:00:00 and the first day of the next month 0:00:00 and extract the number of seconds.


How accurate do you need to be?

60 seconds * 60 minutes * 24 hours * Z days in the month gives you an accurate number for a given month.

If you need an average month go for number of seconds in the year and divide by twelve.

In some domains, such as billing or legal domains a 'month' might actually be exactly 30 days.

If you are working across multiple years or doing tight integration between disperse systems, you'll need to consult resource to determine leap seconds. For historical data this could be a table, but otherwise you'd be better suited by synchronizing to a trusted time source.

http://en.wikipedia.org/wiki/Leap_second


60 (seconds) * 60 (minutes) * 24 (hours) * ## (days in the month)


Given that there are 86,400 seconds in a day, you can multiply this number by the result of the DateTime.DaysInMonth function (in C#). The following function does just that:

public double SecondsInMonth(int year, int month)
{
   return DateTime.DaysInMonth(year, month) * 86400;
}

E.g., find the seconds in the current month:

double secondsInCurrentMonth = SecondsInMonth(DateTime.Now.Year, DateTime.Now.Month);


Number of days in the given month * hours/day * minutes/hour * seconds/minute

is the best way.


If you're doing this in pure math it would be 60 * 60 * 24 * <number of days in month>.

What's the use case?


No, use the date API available for a particular lannguage and determine the number of days in the current month. Then calculate the number of seconds. Also take into account leap years.


Depends on if you want an average month or a specific month....your way gets an average. For a specific month count days and multiply by 86400 (seconds per 24.0 hour day)


This isn't really a programming question. Months have different lengths, so dividing the number of seconds in a year by 12 will give you nothing useful. It's easy to determine the days in a month - a simple lookup table plus a calcualation of leap years will do it. Then just multiply by the number of seconds in a day.

If you are being really precise you might need to include calculations of leap seconds, but since they are unpredictably assigned based on astronimical calculations, and not predictable in advance, I would probably ignore them.


Number of days vary in each month.Proper algorithm for this is to get number of days in moth and multiply it with 86400 (number of seconds in a day).You might also need average count or leap years calculation ...


The trivial answer is to find the number of days in the month and then multiply by 86400. That will work perfectly if you are dealing with dates and times in UTC. However, if you are using a local time zone then this approach yields a slightly incorrect result if that time zone observes daylight saving time. The error is somewhat small over a one month period, but will magnify if you need to make similiar calculation over short periods like a day. I definitely recommend doing all processing and storage in UTC, but depending on the application you will have to convert your UTC times to the local time zone that the end user is expecting. And it might even be plausible that you have to calculate durations using the local time zone. Again, use UTC as much as possible so that you avoid most of the problems.

I came up with this solution in C#. It is compatible with UTC and local time zones alike. You just have to tell the GetNumberOfSecondsInMonth which time zone you want the calculation to be based on. In my example I chose November of 2010 because here in Missouri we observe DST and there was one extra hour this month. Daylight saving time rules change so I used an API that pulls the DST information from the operating system so that the calculation will be correct for years prior to 2007 (that is when the United States expanded DST for most regions).

I should point out that my solution does not handle leap seconds in UTC. For me that is never an issue. But it would be easy to account for that by using a lookup table if you really needed ultra high precision timing.

public class Program
{
    static void Main(string[] args)
    {
        int seconds = GetNumberOfSecondsInMonth(2010, 11, DateTimeKind.Local);
    }

    public static int GetNumberOfSecondsInMonth(int year, int month, DateTimeKind kind)
    {
        DateTime start = new DateTime(year, month, 1);
        DateTime end = start.AddMonths(1);
        int seconds = (int)(end - start).TotalSeconds;
        if (kind == DateTimeKind.Local)
        {
            DaylightTime dt = TimeZone.CurrentTimeZone.GetDaylightChanges(year);
            seconds = (dt.Start > start) ? seconds - 3600 : seconds;
            seconds = (dt.End < end) ? seconds + 3600 : seconds;
        }
        return seconds;
    }
}


It's a problem with years ang months as there is not a fixed number of days in them. But after a lot of thought I have figured out how to do it. It was not a good idea to calculate months with either 30 or 31 days in them, because it looks bad, for example converting from 1 year to months would give an answer of 11 months and 25 days if I had 30 days in each month, or 12 months and 5 days if I have 31 days in each month.

Instead I loop through a series of days per month: 30,30,31,30,31,30,31,30,31,30,31,30 which makes a total of 365 days in a year. So if I want the number of days in 4 months I add 30+30+31+30. And if I start with 23 months it would go through the loop almost twice (23 times 30 or 31). It's done in a while/until loop. For every 4 years I add 1 day, making it 366 days (the first 30 is changed to 31 in the list). It's rather complex but it works and the result looks better.

0

精彩评论

暂无评论...
验证码 换一张
取 消