Jump to content
A 2021 backup has been restored. Forums are closed and work in progress. Join our Discord server for more updates! ×
SoaH City Message Board

PHP Question


Epon

Recommended Posts

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

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. :P

Link to comment
Share on other sites

// 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...