jimfog Posted November 10, 2015 Share Posted November 10, 2015 I want to get the dates of specific weekdays of the current month...for example: today is Tuesday 10 November.... WHat will the date be in remaining Tuesday's of this month? Link to comment Share on other sites More sharing options...
Ingolme Posted November 10, 2015 Share Posted November 10, 2015 You can use strtotime() to get "next Tuesday" and then continue adding seven days at a time until the month is no longer November. Link to comment Share on other sites More sharing options...
jimfog Posted November 11, 2015 Author Share Posted November 11, 2015 (edited) I did get Next Monday...and at the time I wanted. I used this: strtotime("next Monday T12:00");//16th November How am I going to get the Monday now of 23th of November. You say to add seven days at a time...how? Edited November 11, 2015 by jimfog Link to comment Share on other sites More sharing options...
dsonesuk Posted November 11, 2015 Share Posted November 11, 2015 Can you not get say 'last monday', then all you have to do is loop from first or current Monday to last Monday, retriving all those between. Link to comment Share on other sites More sharing options...
jimfog Posted November 11, 2015 Author Share Posted November 11, 2015 (edited) I managed to get the last monday of the month. But how I am going to loop through them? So far we have to strtotime(next Monday, and last Monday). Should I use a method of the DateTime Class to achieve this?And if yes which? I found something...nonetheless you are still welcome to submit your opinion on how to solve this. I FINALLY SET UP THE LOOP. Edited November 11, 2015 by jimfog Link to comment Share on other sites More sharing options...
dsonesuk Posted November 11, 2015 Share Posted November 11, 2015 There is no reference to 'last Monday of this Month' but it works! using normal date strtotime() or DateTime class $start_date = date('Y-m-d', strtotime("Monday")); //$start_date = date('Y-m-d', strtotime("first Monday of this Month")); $end_date = date('Y-m-d', strtotime("last Monday of this Month")); while (strtotime($start_date) <= strtotime($end_date)) { echo $start_date . '<br>'; $start_date = date("Y-m-d", strtotime("next Monday", strtotime($start_date))); } echo '<hr>'; //dateTime Class $getFirstWeeklyDay = new DateTime(date('Y-m-d', strtotime("Monday"))); //$getFirstWeeklyDay = new DateTime(date('Y-m-d', strtotime("first Monday of this Month"))); $getLastWeeklyDay = clone $getFirstWeeklyDay; $getLastWeeklyDay->modify('last Monday of this Month')->format('Y-m-d'); while ($getFirstWeeklyDay->getTimestamp() <= $getLastWeeklyDay->getTimestamp()) { echo $getFirstWeeklyDay->format('Y-m-d') . ' <br>'; $getFirstWeeklyDay->modify('next Monday')->getTimestamp(); } Link to comment Share on other sites More sharing options...
jimfog Posted November 11, 2015 Author Share Posted November 11, 2015 here is the loop I wrote...based on strtotime: $start = strtotime("next Monday T14:00"); $end = strtotime("last Monday of this month T14:00"); $date = $start; while($date <= $end) { echo $date.'<br>'; $date = strtotime("+7 days", $date); } Some notes: Do you think mine or yours is better and why....I think yours does not produce UNIX tmestamps Do you think it is better to use +7 days or Next Monday(i am referring to the code with in the loop) Another think that I noticed is that you are using strtotime within strtotime...(I am again referring to the code inside the loop)..why that. I read in the manual why you might be doing that... Just say what comes to your mind when comparing the 2 different codes. Link to comment Share on other sites More sharing options...
dsonesuk Posted November 11, 2015 Share Posted November 11, 2015 Looks like mine is setup to gather and show formated date, and convert to timestamp within while loop condition, the second strtotime holds current next in this case 'monday', probably using '+ 7‘ would be better, as you will not just targeting 'mondays'. You may have problem targeting current Monday when using 'next monday' unless that is what you want? Link to comment Share on other sites More sharing options...
jimfog Posted November 12, 2015 Author Share Posted November 12, 2015 Imagine now the following(what I am about to say I did not want to mention it from the beginning due to its complexity-I chose a step by step process here).... I want the timestamps from current Monday till last Monday...but only from 10:00 to 15:00 at half hour intervals (this is an example). Am I clear? Link to comment Share on other sites More sharing options...
dsonesuk Posted November 12, 2015 Share Posted November 12, 2015 (edited) As in <?php $start_date = strtotime("Monday 10:00:00"); $end_date = strtotime("last Monday of this Month 15:00"); while ($start_date <= $end_date) { while (date("H", $start_date) >= 10 && date("H", $start_date) < date("H", $end_date)) { echo $start_date . ' = ' . date('D Y-m-d H:i', $start_date) . '<br>'; $start_date = strtotime("+30 minutes", $start_date); } echo $start_date . ' = ' . date('D Y-m-d H:i', $start_date) . '<br>'; $start_date = strtotime("+ 7 days 10:00:00", $start_date); } //DateTime Class echo '<hr>'; $getFirstWeeklyDay = new DateTime(date('Y-m-d H:i', strtotime("Monday 10:00:00"))); //$getFirstWeeklyDay = new DateTime(date('Y-m-d', strtotime("first Monday of this Month"))); $getLastWeeklyDay = clone $getFirstWeeklyDay; $getLastWeeklyDay->modify('last Monday of this Month 15:00')->format('Y-m-d H:i'); while ($getFirstWeeklyDay->getTimestamp() <= $getLastWeeklyDay->getTimestamp()) { while ($getFirstWeeklyDay->format('H') >= 10 && $getFirstWeeklyDay->format('H') < $getLastWeeklyDay->format('H')) { echo $getFirstWeeklyDay->getTimestamp() . ' = ' . $getFirstWeeklyDay->format('D Y-m-d H:i') . ' <br>'; $getFirstWeeklyDay->modify('+ 30 minutes')->getTimestamp(); } echo $getFirstWeeklyDay->getTimestamp() . ' = ' . $getFirstWeeklyDay->format('D Y-m-d H:i') . ' <br>'; $getFirstWeeklyDay->modify("+ 7 days 10:00:00")->getTimestamp(); } ?> Edited November 12, 2015 by dsonesuk Link to comment Share on other sites More sharing options...
jimfog Posted November 12, 2015 Author Share Posted November 12, 2015 this is what I want...yes. The only problem is that the last Monday of the month(30th November) is left out...I do not know why. I see the timestamps only for the 16th and 23th of November. Link to comment Share on other sites More sharing options...
dsonesuk Posted November 12, 2015 Share Posted November 12, 2015 (edited) Err..? it shows the 30th when i run it 1447664400 = Mon 2015-11-16 10:00 1447666200 = Mon 2015-11-16 10:30 1447668000 = Mon 2015-11-16 11:00 1447669800 = Mon 2015-11-16 11:30 1447671600 = Mon 2015-11-16 12:00 1447673400 = Mon 2015-11-16 12:30 1447675200 = Mon 2015-11-16 13:00 1447677000 = Mon 2015-11-16 13:30 1447678800 = Mon 2015-11-16 14:00 1447680600 = Mon 2015-11-16 14:30 1447682400 = Mon 2015-11-16 15:00 1448269200 = Mon 2015-11-23 10:00 1448271000 = Mon 2015-11-23 10:30 1448272800 = Mon 2015-11-23 11:00 1448274600 = Mon 2015-11-23 11:30 1448276400 = Mon 2015-11-23 12:00 1448278200 = Mon 2015-11-23 12:30 1448280000 = Mon 2015-11-23 13:00 1448281800 = Mon 2015-11-23 13:30 1448283600 = Mon 2015-11-23 14:00 1448285400 = Mon 2015-11-23 14:30 1448287200 = Mon 2015-11-23 15:00 1448874000 = Mon 2015-11-30 10:00 1448875800 = Mon 2015-11-30 10:30 1448877600 = Mon 2015-11-30 11:00 1448879400 = Mon 2015-11-30 11:30 1448881200 = Mon 2015-11-30 12:00 1448883000 = Mon 2015-11-30 12:30 1448884800 = Mon 2015-11-30 13:00 1448886600 = Mon 2015-11-30 13:30 1448888400 = Mon 2015-11-30 14:00 1448890200 = Mon 2015-11-30 14:30 1448892000 = Mon 2015-11-30 15:00 -------------------------------------------------------------- 1447664400 = Mon 2015-11-16 10:00 1447666200 = Mon 2015-11-16 10:30 1447668000 = Mon 2015-11-16 11:00 1447669800 = Mon 2015-11-16 11:30 1447671600 = Mon 2015-11-16 12:00 1447673400 = Mon 2015-11-16 12:30 1447675200 = Mon 2015-11-16 13:00 1447677000 = Mon 2015-11-16 13:30 1447678800 = Mon 2015-11-16 14:00 1447680600 = Mon 2015-11-16 14:30 1447682400 = Mon 2015-11-16 15:00 1448269200 = Mon 2015-11-23 10:00 1448271000 = Mon 2015-11-23 10:30 1448272800 = Mon 2015-11-23 11:00 1448274600 = Mon 2015-11-23 11:30 1448276400 = Mon 2015-11-23 12:00 1448278200 = Mon 2015-11-23 12:30 1448280000 = Mon 2015-11-23 13:00 1448281800 = Mon 2015-11-23 13:30 1448283600 = Mon 2015-11-23 14:00 1448285400 = Mon 2015-11-23 14:30 1448287200 = Mon 2015-11-23 15:00 1448874000 = Mon 2015-11-30 10:00 1448875800 = Mon 2015-11-30 10:30 1448877600 = Mon 2015-11-30 11:00 1448879400 = Mon 2015-11-30 11:30 1448881200 = Mon 2015-11-30 12:00 1448883000 = Mon 2015-11-30 12:30 1448884800 = Mon 2015-11-30 13:00 1448886600 = Mon 2015-11-30 13:30 1448888400 = Mon 2015-11-30 14:00 1448890200 = Mon 2015-11-30 14:30 1448892000 = Mon 2015-11-30 15:00 Edited November 12, 2015 by dsonesuk Link to comment Share on other sites More sharing options...
jimfog Posted November 12, 2015 Author Share Posted November 12, 2015 yes you are right...I could not see the rest cause the container div needed height adjustment.... And lastly I just want to pass all these into an array so that they can be returned from a within a function. Link to comment Share on other sites More sharing options...
dsonesuk Posted November 12, 2015 Share Posted November 12, 2015 You are capable of doing that right? create array, count as each timestamp is created starting from 0 and use count as index by incrementing it after each loop. Link to comment Share on other sites More sharing options...
jimfog Posted November 12, 2015 Author Share Posted November 12, 2015 I tried but I cannot...I am stuck...sometimes I even stuck at things I have done before. Link to comment Share on other sites More sharing options...
dsonesuk Posted November 12, 2015 Share Posted November 12, 2015 $intervalArray = []; //$start_date = strtotime("Monday 10:00:00"); $start_date = strtotime("2 November 2015 10:00:00"); $end_date = strtotime("last Monday of this Month 15:00"); while ($start_date <= $end_date) { while (date("H", $start_date) >= 10 && date("H", $start_date) < date("H", $end_date)) { echo $start_date . ' = ' . date('D Y-m-d H:i', $start_date) . '<br>'; array_push($intervalArray, $start_date); $start_date = strtotime("+30 minutes", $start_date); } array_push($intervalArray, $start_date); echo $start_date . ' = ' . date('D Y-m-d H:i', $start_date) . '<br>'; $start_date = strtotime("+30 minutes", $start_date); $start_date = strtotime("+ 7 days 10:00:00", $start_date); } echo '<hr>'; echo ' <p> From Array - ' . count($intervalArray) . ' intervals stored</p>'; foreach ($intervalArray as $interval) { echo $interval . ' = ' . date('D Y-m-d H:i', $interval) . '<br>'; } //DateTime Class echo '<hr> <p> From DateTime Class</p>'; $getFirstWeeklyDay = new DateTime(date('Y-m-d H:i', strtotime("Monday 10:00:00"))); //$getFirstWeeklyDay = new DateTime(date('Y-m-d', strtotime("first Monday of this Month"))); $getLastWeeklyDay = clone $getFirstWeeklyDay; $getLastWeeklyDay->modify('last Monday of this Month 15:00')->format('Y-m-d H:i'); while ($getFirstWeeklyDay->getTimestamp() <= $getLastWeeklyDay->getTimestamp()) { while ($getFirstWeeklyDay->format('H') >= 10 && $getFirstWeeklyDay->format('H') < $getLastWeeklyDay->format('H')) { echo $getFirstWeeklyDay->getTimestamp() . ' = ' . $getFirstWeeklyDay->format('D Y-m-d H:i') . ' <br>'; $getFirstWeeklyDay->modify('+ 30 minutes')->getTimestamp(); } echo $getFirstWeeklyDay->getTimestamp() . ' = ' . $getFirstWeeklyDay->format('D Y-m-d H:i') . ' <br>'; $getFirstWeeklyDay->modify("+ 7 days 10:00:00")->getTimestamp(); } Link to comment Share on other sites More sharing options...
jimfog Posted November 12, 2015 Author Share Posted November 12, 2015 thanks a lot...this what I tried and it did not worked. I did not use array_push...I simply did this: ... $timestamps=array(); whiile (date("H", $start_date) >= 10 && date("H", $start_date) < 15) { echo $start_date . ' = ' . date('D Y-m-d H:i', $start_date) . '<br>'; $timestamps= strtotime("+30 minutes", $start_date); ... why the above could not work? I just added to the timestamps array a timestamp on each iteration. Link to comment Share on other sites More sharing options...
dsonesuk Posted November 12, 2015 Share Posted November 12, 2015 whiile (date("H", $start_date) >= 10 && date("H", $start_date) < 15) and to insert value in array without push you use $timestamps[]= strtotime("+30 minutes", $start_date); but you are selecting the wrong item to insert? you require the same value that is echoed, not the value that increases by 30mins echo $start_date . ' = ' . date('D Y-m-d H:i', $start_date) . '<br>'; $timestamps[]= $start_date; Link to comment Share on other sites More sharing options...
dsonesuk Posted November 13, 2015 Share Posted November 13, 2015 (edited) Better option with ability to change start and end time periods, and rename incrementing variable name to give better description on what it does, also method of inserting values in array using normal and array_push(). <?php $intervalArray = []; $intervalArray2 = []; $startTimePeriod = "10:00"; $endTimePeriod = "15:00"; //$start_date = strtotime("Monday " . $startTimePeriod); $start_date = strtotime("2 November 2015 " . $startTimePeriod); $end_date = strtotime("last Monday of this Month " . $endTimePeriod); $intervalPeriod = $start_date; echo '<h4> Start Period Times = ' . $startTimePeriod . ' : End Period Times = ' . $endTimePeriod . ' ONLY </h4>'; while ($intervalPeriod <= $end_date) { while (date("Hi", $intervalPeriod) >= (int) str_replace(':', '', $startTimePeriod) && date("Hi", $intervalPeriod) < (int) str_replace(':', '', $endTimePeriod)) { echo $intervalPeriod . ' = ' . date('D Y-m-d H:i', $intervalPeriod) . '<br>'; array_push($intervalArray, $intervalPeriod); $intervalPeriod = strtotime("+30 minutes", $intervalPeriod); } array_push($intervalArray, $intervalPeriod); echo $intervalPeriod . ' = ' . date('D Y-m-d H:i', $intervalPeriod) . '<br>'; $intervalPeriod = strtotime("+30 minutes", $intervalPeriod); $intervalPeriod = strtotime("+ 7 days " . $startTimePeriod, $intervalPeriod); } echo '<hr>'; echo ' <p> From Array - ' . count($intervalArray) . ' intervals stored</p>'; foreach ($intervalArray as $interval) { echo $interval . ' = ' . date('D Y-m-d H:i', $interval) . '<br>'; } //DateTime Class echo '<hr> <p> From DateTime Class</p>'; $getFirstWeeklyDay = new DateTime(date('Y-m-d H:i', strtotime("Monday " . $startTimePeriod))); //$getFirstWeeklyDay = new DateTime(date('Y-m-d', strtotime("first Monday of this Month " . $startTimePeriod))); $getLastWeeklyDay = clone $getFirstWeeklyDay; $getLastWeeklyDay->modify("last Monday of this Month " . $endTimePeriod)->format('Y-m-d H:i'); $intervalPeriod = clone $getFirstWeeklyDay; while ($intervalPeriod->getTimestamp() <= $getLastWeeklyDay->getTimestamp()) { while ($intervalPeriod->format('Hi') >= (int) str_replace(':', '', $startTimePeriod) && $intervalPeriod->format('Hi') < (int) str_replace(':', '', $endTimePeriod)) { echo $intervalPeriod->getTimestamp() . ' = ' . $intervalPeriod->format('D Y-m-d H:i') . ' <br>'; $intervalArray2[] = $intervalPeriod->getTimestamp(); $intervalPeriod->modify('+ 30 minutes')->getTimestamp(); } $intervalArray2[] = $intervalPeriod->getTimestamp(); echo $intervalPeriod->getTimestamp() . ' = ' . $intervalPeriod->format('D Y-m-d H:i') . ' <br>'; $intervalPeriod->modify("+ 7 days " . $startTimePeriod)->getTimestamp(); } echo '<hr>'; echo ' <p> From Array2 - ' . count($intervalArray2) . ' intervals stored</p>'; foreach ($intervalArray2 as $interval) { echo $interval . ' = ' . date('D Y-m-d H:i', $interval) . '<br>'; } ?> Edited November 13, 2015 by dsonesuk Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now