|
Joined: Jan 2003
Posts: 125
Journeyman
|
Journeyman
Joined: Jan 2003
Posts: 125 |
i'm building a report at work against a MS SQL database with financial info in it. my boss is desiring the report to take, for example, today's date, and compare it to the same "DAY" last year.
so if we ran a report for today: Tues March 11, 2003 it would compare against Tues March 12, 2002.
It seems simple except for Leap Years. Anybody have any advice on the best practice to do this. I'm thinking convert to time stamp and do some IF conditions to determine if a Leap year is involved.
Any help would be appreciated.
Chillin
|
|
|
|
Joined: Apr 2002
Posts: 1,768
Addict
|
Addict
Joined: Apr 2002
Posts: 1,768 |
I don't know about MS SQL, but MySQL has some column types specifically for storing dates and times. If you're using one of those, you can make use of the Date and Time Functions, such as DATE_SUB.
|
|
|
|
Joined: Jan 2003
Posts: 125
Journeyman
|
Journeyman
Joined: Jan 2003
Posts: 125 |
unfortunately, the dates in this particular db/tables are stored as YYYY-MM-DD, i have a stored procedure that does somethign similar as to what i'd like to do.
i'll try to hack it to work with php and post
|
|
|
|
Joined: Aug 2002
Posts: 1,191
Kahuna
|
Kahuna
Joined: Aug 2002
Posts: 1,191 |
A good way to see if a year is a leap one is to divide it by 4. If the modulus is 0 then it is a leap year, hence 2000, 2004, 2008 are whereas 2001, 2002, 2003 etc. are not.
Nikos
|
|
|
|
Joined: May 1999
Posts: 1,715
Addict
|
Addict
Joined: May 1999
Posts: 1,715 |
Actually, it is not quite that easy, but it'll be a while before that calculation won't work again. =]
|
|
|
|
Joined: Aug 2002
Posts: 1,191
Kahuna
|
Kahuna
Joined: Aug 2002
Posts: 1,191 |
Alternatively you can use the mcal library ( http://mcal.chek.com) which introduces the mcal_is_leap_year. You pass the year as an integer and it returns true or false depending on whether the year is a leap one or not.
Nikos
|
|
|
|
Joined: May 1999
Posts: 1,715
Addict
|
Addict
Joined: May 1999
Posts: 1,715 |
Or just this: date("L", mktime(0,0,0,1,1,2004)) which will be true or false depending on if the year is a leap year or not.
In this case, use date("L", strtotime($datefromdb))
|
|
|
|
Joined: Aug 2002
Posts: 1,191
Kahuna
|
Kahuna
Joined: Aug 2002
Posts: 1,191 |
Nikos
|
|
|
|
Joined: May 1999
Posts: 1,715
Addict
|
Addict
Joined: May 1999
Posts: 1,715 |
|
|
|
Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.
Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
|
|
Posts: 87
Joined: December 2001
|
|
Forums63
Topics37,575
Posts293,931
Members13,824
|
Most Online6,139 Sep 21st, 2024
|
|
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
|
|
|
|