0

I am looking on how to get specific day from time given :

let's say I have 2 dates : 1 Jan 2016 until 1 Jan 2017, I will need to know how many Mondays are there, or Tuesdays or any days that I select on the input I gave.

Is there any best way to achieve this ?

bit
  • 4,407
  • 1
  • 28
  • 50
Boby
  • 63
  • 1
  • 2
  • 11
  • Get all dates as descripbe here - http://stackoverflow.com/questions/14851190/date-function-to-display-all-dates-between-two-dates –  Feb 03 '16 at 05:39
  • 1
    get day from date here- http://stackoverflow.com/questions/1385801/how-can-i-get-the-day-of-a-specific-date-with-php –  Feb 03 '16 at 05:39
  • Then you can filter array and get date –  Feb 03 '16 at 05:40

3 Answers3

2

UPDATED ANSWER

<?php
$count = 0;
$days = array("Monday", "Friday");
foreach ($days as $day) {

    $startDate = '1 Jan 2016';
    $endDate = '1 Jan 2017';

    $endDate = strtotime($endDate);
    $i = '';
    for ($i = strtotime($day, strtotime($startDate)); $i <= $endDate; $i = strtotime('+1 week', $i)) {
        echo date('l Y-m-d', $i) . '<br>';
        $count++;
    }
}
echo $count;
Ali Zia
  • 3,825
  • 5
  • 29
  • 77
0

Try modify this example:

<?php
//http://php.net/manual/en/class.dateperiod.php#109846
$begin = new DateTime( '2012-08-01' );
$end = new DateTime( '2012-08-31' );
$end = $end->modify( '+1 day' );

$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);

foreach($daterange as $date){
    //HERE ADD YOUR CONDITIONS...
    echo $date->format("Ymd") . "<br>";
}
?>

You can reviewer this solution PHP create range of dates

Community
  • 1
  • 1
0

get all days and loop through them all, get the first Monday after the start date and then iterate 7 days at a time:

    $endDate = strtotime($endDate);
    for($i = strtotime('Monday', strtotime($startDate)); $i <= $endDate; $i =strtotime('+1 week', $i))
    echo date('l Y-m-d', $i);