-1

I'm trying to figure out what a date(m-d-Y) would be for a specific week. The date that I'm trying to calculate would be passed in as the day string ('monday', 'tuesday', etc...) and the week would be a PHP DatePeriod object. I will be passing in a specific date and I would like to figure out any date of that DatePeriod week. Can anyone please help me?

EDIT: Sorry, I was struggling with how to ask this. Let's say I need to add an event to a calendar that will recur every week for a month. I have an initial date (ex. 7-23-2015) and I need to add an event for monday, tuesday and thursday for that week and every week after, for the month. So I need to figure out what the date would be for that day in whatever week that I am in. Does that make sense?

Lagnor
  • 3
  • 3
  • no idea what you're getting at. a dateperiod is just a span of datetime, e.g. `jan1 -> mar28`. exactly WHICH of the many mondays would you expect to get told from that? – Marc B Jul 22 '15 at 20:03
  • Can we assume the date period is a 7-day period, and that you want to know the date of the Wednesday (for example) that falls in that period? – Jack Albright Jul 22 '15 at 20:10
  • @JackAlbright I supposed so, when write the answer. I hope we are right :) – splash58 Jul 22 '15 at 20:14
  • @JackAlbright Yes, that's exactly what I would like to know. Sorry for the poorly worded question. – Lagnor Jul 23 '15 at 15:31
  • @Lagnor, try what splash58 suggested. I haven't tested it, but it looks right to me. – Jack Albright Jul 24 '15 at 13:10

1 Answers1

0
// Making test period
$begin = new DateTime( '2015-07-20' );
$end = new DateTime( '2015-07-27' );
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
// Array of day names    
$days = ['monday', 'tuesday', ... ];
// String we looking for
$string = 'tuesday';
// day of week - monday is zero  
$i = array_search($string, $days);

if ($i !== false) {
   // add i to find needed day
   echo $daterange->start->modify( "+$i day" )->format("Y-m-d");
}
splash58
  • 26,043
  • 3
  • 22
  • 34