Epon Posted January 26, 2009 Report Share Posted January 26, 2009 Hello there chaps. I'm fiddling around with the date() and mktime() functions and its late and I can't wrap my head around this one. Anyone who helps me out gets some form of rep points or whatever it is we give out here (Slingerland said HJ's, but I'm not into that) Anyway, let's say I have a date, we'll call it $today. I want to be able to calculate last years date based on the DAY OF WEEK and WEEK # of year. If that doesn't make sense, let me explain... Today is 1/26/2009, it is a Monday. We are in the 5th week of January. I want to know what the Unix Timestamp is of last years Monday in the 5th week of January (the answer is 1/28/2008, but I need an algorithmic way of solving this) I know there's a sodding way but I can't quite get it. Somehow I have to get mktime() and date() to bumfsck each other perfectly, without lube. Anyone? Thanks. Link to comment Share on other sites More sharing options...
hRook Posted January 26, 2009 Report Share Posted January 26, 2009 Took me a little less than an hour. A little more complicated than you thought it'd be probably, but it's well documented so you should be able to make sense out of it. http://www.hrook.net/dickin/eponDateFunc.php http://www.hrook.net/dickin/eponDateFunc.phps 1 Link to comment Share on other sites More sharing options...
Smidge204 Posted January 26, 2009 Report Share Posted January 26, 2009 I'm assuming you just want to find the same "square" on the calendar one year ago? eg: If the calendar was one grid 7 columns wide by 53 rows long, what's the date using last year's numbers? Let me know if that's what you want, because it's pretty simple if it is. =Smidge= 1 Link to comment Share on other sites More sharing options...
Epon Posted January 26, 2009 Author Report Share Posted January 26, 2009 Smidge, yes that's what I'm looking for, but I need it to work in all cases, including leap years, etc. If you can get me something smaller than Hunter's, than I'll use it, but don't kill yourself. Hunter: Ridiculous. Thank you. A bit long, but I think it'll get the job done. Very good documentation, as well. Link to comment Share on other sites More sharing options...
Smidge204 Posted January 26, 2009 Report Share Posted January 26, 2009 // Input: UNIX timestamp // Output: UNIX timestamp // Currently only works for one-year differential. Suggest using multiple // times for multiple years. function this_day_in_history($t) { $dd = explode(" ", date("n j Y", $t)); // Leap year correction: Add one day if (date("L", $t) == 1 && date("z", $t) >=60) $dd[1]++; if (date("L", mktime(0,0,0,1,1,$dd[2]-1)) == 1 && date("z", $t) <= 59) $dd[1]++; return mktime(0,0,0,$dd[0],$dd[1]-364, $dd[2]); } Let me know if that works as expected. Edit: Bugfix... =Smidge= Link to comment Share on other sites More sharing options...
Epon Posted January 27, 2009 Author Report Share Posted January 27, 2009 Works like a charm, Smudge. Thanks! Hunter, thanks for the help though, I do like your elegance. Link to comment Share on other sites More sharing options...
Recommended Posts