Othman
Othman

Reputation: 342

get the last thursday before a date in PHP

I know there is similar post about how to get the last Thursday in PHP but I don't want to have the last Thursday compare to the current date but the last Thursday compare to a given date.

For example I have a date dd/mm/yyyy and I want the Thursday before this date. The input is a String ( the format of the string yymmdd) that I want to parse to get the Thursday before this date.

Thanks for your help

Upvotes: 3

Views: 3829

Answers (2)

cutsoy
cutsoy

Reputation: 10251

$day = date('w', $yourTime);
$time = $yourTime - ($day > 4 ? ($day - 4) : ($day + 7 - 4)) * 3600 * 24;

Where both $yourTime and $time are Unix-timestamps.

Edit: @Rudu's solution is way more simple, you should stick with that one :)!

Upvotes: 3

Rudu
Rudu

Reputation: 15892

//Assumes it's strtotime parsable, you may need to insert
//  slashes with your given format eg (and use 4 digit years)
$given=strtotime($dtstring);

//It's just that easy ;)
$thuBefore=strtotime("last thursday",$given);

Note that this will always get last thursday, meaning if the given date is a Thursday, it'll report 7 days earlier (but if the date's a Friday it'll only report one day earlier).

Upvotes: 9

Related Questions