I’m writing a script that gets some statistics about my website for a date this year, but I also want it to get me the data for the nearest corresponding day last year so I can compare them.
For example if I were to get the data for “Wednesday 14th Dec 2011”, I’d want to also get the data for “Wednesday 15th Dec 2010”.
I’m having a bit of trouble thinking of how to get the correct date from this year’s date. I’d prefer to be able to pass the data into a function, something like:
// $date as a unix timestamp
// $day (0 for Sunday through 6 for Saturday)
function getClosestDay($date,$day=0) {
}
So I would be passing in a unix timestamp of last year’s date, and also the day I’m looking to find. I’d expect it to return a unix timestamp of the correct day.
But I’m not sure where to even start with the function.
I’m not looking for someone to write the function for me, but if anyone has any ideas on where to start (even a nudge in the right direction) then that would be great!
I believe this would do. First we get the unix time of the same day last year
Now we find the difference in week day. In this example, it’ll be -1
And then we extract/add that difference to the date
And output it
The above should yield