3

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

Othman
  • 332
  • 5
  • 19
  • 020512 means 12 May 2002 , I want to parse this string to get the Thursday before and return the date of the Thursday before in the same format (yymmdd) – Othman Jul 27 '11 at 15:06
  • @Shef that is usually handled in php.ini and will resolve itself. – Brian Driscoll Jul 27 '11 at 15:06
  • @Oto What about if the date passed to the function happens to be a Thursday; should the function return the same date or the date a week prior? – Brian Driscoll Jul 27 '11 at 15:08
  • 1
    @Shef the fact that it's a string is of no consequence; choosing a century from a 2-digit year is a configuration issue and is thus handled in php.ini. – Brian Driscoll Jul 27 '11 at 15:09
  • 1
    @Shef you do know that you can tell PHP what format the string is in for date parsing purposes, yes? – Brian Driscoll Jul 27 '11 at 15:10

2 Answers2

9
//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).

Rudu
  • 15,682
  • 4
  • 47
  • 63
  • +1 ...but beware of the way `strtotime` functions in different versions of PHP. Be sure to check the [Changelog](http://www.php.net/manual/en/function.strtotime.php#refsect1-function.strtotime-changelog) for issues relating to relative dates, and test carefully if using more than one version of PHP. – Mike Jul 27 '11 at 15:18
3
$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 :)!

cutsoy
  • 10,127
  • 4
  • 40
  • 57
  • @TvE Thanks! I always used to do it the manual way (although by attempting to avoid branches with MOD/%) until I discovered there's some very handy built in ability. I wonder how the two approaches compare on performance? – Rudu Jul 27 '11 at 15:33
  • I thought about using MOD, because it looks much nicer, but I didn't really figure out how to do that. MOD basically is x - (floor(x) / y) * y, but I can't really figure out how to place that in this 'formula'. But I'm pretty sure MOD does have a better performance, though the difference won't be that significant :). – cutsoy Jul 27 '11 at 16:54
  • Oh, I didn't know you meant comparing `MOD` vs `()`-braces or `MOD`/braces vs `strtotime`. I'm pretty sure plain math is faster than `strtotime`. I've been told that `strtotime` creates an internal Date-object that performs all calculations, so working with integers is probably a lot faster :)! Besides that, it also has to parse your 'command'-string ("last thursday"), which also does need some more time than just integers. However, it's much easier to understand and read for (other) developers than such an formula, so ... :)! – cutsoy Jul 27 '11 at 16:57