Forum Moderators: coopster

Message Too Old, No Replies

Changing the definition of a month

         

doodlebee

2:13 pm on May 18, 2010 (gmt 0)

10+ Year Member



Hello all!

I was wondering if someone could point me in the right direction. I need to redefine what a "month" is. For example, the month of May would typically run from May 1 through to May 31. I need to "offset" that so the month of may actually starts in March, (I know, it sounds weird.) so May2010 would actually consist of items dated from March 20, 2010 through to April 19th, 2010.

the "19th" and "20th" are the only set standards. So, April 2010 would be February 20th through March 19th; March 2010 would be January 20th through February 19th, and so on.

Would anyone have any ideas on how to pull this off? The dates are placed in the database in this format:

2010-04-07 12:08:30

So I figured I could do some kind of DB query that would look at these date/timestamps and just "offset" what gets pulled. (I'm trying to create archive links).

Any ideas or help would be appreciated :)

jatar_k

2:23 pm on May 18, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



unix timestamps might make the date math faster
also look at [php.net...]

as long as your offset is static then you can make a function to convert the dates on the fly

doodlebee

2:32 pm on May 18, 2010 (gmt 0)

10+ Year Member



Thank you! I will check that out - It might be just what I need :)

rocknbil

5:37 pm on May 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



(Second one today!) If you're using mySQL, the date and time functions will be extremely useful in this, expecially when it comes to sorting and limit (for pagination.) You'll have to fiddle with it, decide whether you want to offset by days or months or a combination of both, but start playing with these.

May2010 would actually consist of items dated from March 20, 2010 through to April 19th, 2010


// Get the DAYS from March 20 to May 1, store in $diff
select datediff('2010-05-01','2010-03-20');
--> 42 (days)

select your_date_field from your_table where your_date_field >= date_sub('2010-05-01',interval $diff day) and your_date_field <= date_add(date_sub('2010-05-01',interval $diff day),1 month) order by your_date_field desc limit $per_page;

... where $per_page is your per page limit, in which case you'll have a pagination scheme in place . . .

Should give you all the dates from March 20 to "one month later." The varying lengths of months are automatically calculated, including leap years.

The hard coded date '2010-05-01' can be a database field, and it will work as well on datetime types, but you may have to do some fiddling with where midnight lands.

Date and time functions [dev.mysql.com] (5.0 version, all about the same)

doodlebee

7:26 pm on May 18, 2010 (gmt 0)

10+ Year Member



Thank you so much rocknbil! (sorry, I must have missed the previous one - I did look before posting, I swear!)

rocknbil

1:15 am on May 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I just meant, second post today where someone was going against the grain in a sense, trying to get PHP to do stuff mySQL makes so much easier . . .

doodlebee

11:53 am on May 19, 2010 (gmt 0)

10+ Year Member



Oh! I see, okay. :)

Well I was actually looking for *any* solution. I'm using PHP scripting, but every time I did a search for what I wanted to do, I kept getting weird results. So I didn't even know what I was asking for - that's why I asked!