Date and time is one function which is always like there in whatever website, project you create. The more simple they look like to start with the more complex they become. Complexity comes in when we put these functions into different loops conditions etc. Well Today i got a bug in some code and it was named as year 2009 bug. It isnt a universal 2k9 bug but it was just cause of the bad code.

So i eliminated that and cleaned the code, now i am gonna show you how we can play with the unix timestamp function of PHP time()

AIM: We will go to a date say a week or 2 weeks back by using a customized function.

echo date('Y-m-d',gotoWeek(14,"01-01-2009")); //returns 14 weeks back date from the specified date
echo date('Y-m-d',gotoWeek(5)); // returns 5 weeks back date from now
 
function gotoWeek($no_of_weeks,$from="")
{
    $from=(trim($from)=="")?time():strtotime($from);
    $no_of_weeks=($no_of_weeks<=0)?1:$no_of_weeks;
    $week=(int)24*3600*7*$no_of_weeks;
    return $from-$week;
 
}

The above two lines show you how you can call function.
The function is pretty basic, 24*3600*7 , gives us one week. Rest all is pretty naive. Hope this would help you solving any of your bug. Comments, feedback welcome 🙂

Cheers !!
Realin !

Share this post: