I need help with dating
Here are two functions that do exactly that: Hope somebody finds this useful.Gary # function weeknumber /* d a t e f r o m w e e k ---------------------------------- // From a weeknumber, calculates the corresponding date Input: Year, weeknumber and day offset Output: Exact date in an associative (named) array 2003, 12, 0: 2003-03-17 (a Monday) 1995, 53, 2: 1995-12-xx ...
When debugging code that stores date/time values in a database, you may find yourself wanting to know the date/time that corresponds to a given unix timestamp, or the timestamp for a given date & time.The following script will do the conversion either way.If you give it a numeric timestamp, it will display the corresponding date and time.If you give it a date and time (in almost any standard format), it will display the timestamp.This date Diff() function can take in just about any timestamp, including UNIX timestamps and anything that is accepted by strtotime().It returns an array with the ability to split the result a couple different ways.
I built this function to suffice any datediff needs I had. negative integers are returned * * $split recognizes the following: * 'yw' = splits up years, weeks and days (default) * 'y' = splits up years and days * 'w' = splits up weeks and days * 'd' = total days * * examples: * $dif1 = date Diff() or date Diff('yw') * $dif2 = date Diff('y') * $dif3 = date Diff('w') * $dif4 = date Diff('d') * * assuming date Diff returned 853 days, the above * examples would have a print_r output of: * $dif1 == Array( [y] = I needed to calculate the week number from a given date and vice versa, where the week starts with a Monday and the first week of a year may begin the year before, if the year begins in the middle of the week (Tue-Sun).
This is the way weekly magazines calculate their issue numbers.
All conversions are done for your locale/time zone.
Someone may find this info of some use: Rules for calculating a leap year:1) If the year divides by 4, it is a leap year (1988, 1992, 1996 are leap years)2) Unless it divides by 100, in which case it isn't (1900 divides by 4, but was not a leap year)3) Unless it divides by 400, in which case it is actually a leap year afterall (So 2000 was a leap year).
In practical terms, to work out the number of days in X years, multiply X by 365.2425, rounding DOWN to the last whole number, should give you the number of days.
The result will never be more than one whole day inaccurate, as opposed to multiplying by 365, which, over more years, will create a larger and larger deficit.