11
$start_date = "2013-05-01";
$last_date = "2013-08-30";

How can I get dates of tuesdays and thursdays between these two dates?

Itai Klapholtz
  • 196
  • 1
  • 2
  • 15
yajay
  • 1,050
  • 10
  • 24

9 Answers9

10
<?php
$start    = new DateTime('2013-05-01');
$end      = new DateTime('2013-08-30');
$interval = DateInterval::createFromDateString('1 day');
$period   = new DatePeriod($start, $interval, $end);

foreach ($period as $dt) {
    if ($dt->format("N") == 2 || $dt->format("N") == 4) {
        echo $dt->format("l Y-m-d") . "<br>\n";
    }
}

See it in action

What this code does:

  1. Creates a starting date object using DateTime.
  2. Creates a starting date object using DateTime.
  3. Creates a DateInterval object to represent our interval of time to iterate through. In this case 1 day.
  4. Creates a DatePeriod object to manage these objects.
  5. Using DatePeriod, it iterates through the date starting with the starting date and ending at the end date. We use DateTime::format() with the N parameter to get the day number of the week. If the day number of the week is 2 (Tuesday) or 4 (Thursday) echo out it's value.
John Conde
  • 217,595
  • 99
  • 455
  • 496
4

Some PHP-Fu

$start_date = '2013-05-01';
$last_date = '2013-08-30';

$dates = range(strtotime($start_date), strtotime($last_date),86400);
$days = array('tuesday' => array(), 'thursday' => array());

array_map(function($v)use(&$days){
        if(date('D', $v) == 'Tue'){
            $days['tuesday'][] = date('Y-m-d', $v);
        }elseif(date('D', $v) == 'Thu'){
            $days['thursday'][] = date('Y-m-d', $v);
        }
    }, $dates); // Requires PHP 5.3+

print_r($days);

Output

Array
(
    [tuesday] => Array
        (
            [0] => 2013-05-07
            [1] => 2013-05-14
            [2] => 2013-05-21
            [3] => 2013-05-28
            [4] => 2013-06-04
            [5] => 2013-06-11
            [6] => 2013-06-18
            [7] => 2013-06-25
            [8] => 2013-07-02
            [9] => 2013-07-09
            [10] => 2013-07-16
            [11] => 2013-07-23
            [12] => 2013-07-30
            [13] => 2013-08-06
            [14] => 2013-08-13
            [15] => 2013-08-20
            [16] => 2013-08-27
        )

    [thursday] => Array
        (
            [0] => 2013-05-02
            [1] => 2013-05-09
            [2] => 2013-05-16
            [3] => 2013-05-23
            [4] => 2013-05-30
            [5] => 2013-06-06
            [6] => 2013-06-13
            [7] => 2013-06-20
            [8] => 2013-06-27
            [9] => 2013-07-04
            [10] => 2013-07-11
            [11] => 2013-07-18
            [12] => 2013-07-25
            [13] => 2013-08-01
            [14] => 2013-08-08
            [15] => 2013-08-15
            [16] => 2013-08-22
            [17] => 2013-08-29
        )

)

Online demo

HamZa
  • 14,671
  • 11
  • 54
  • 75
2
$start_date = strtotime("2013-05-01");
$last_date = strtotime("2013-08-30");
while ($start_date <= $last_date) {
    $start_date = strtotime('+1 day', $start_date);
    if (date('N',$start_date) == 2 || date('N',$start_date) == 4){
        echo date('Y-m-d', $start_date).PHP_EOL;
    }
}
k102
  • 7,861
  • 7
  • 49
  • 69
  • @Steffi Please could you mark an answer correct on your [previous question](http://stackoverflow.com/questions/14836956/how-to-get-user-image-with-twitter-api-1-1/) if any? Thanks :) – Jimbo Jun 18 '13 at 08:36
1

<?php echo date('Y-m-d', strtotime('next thursday', strtotime($start_date)));

Also for tuesday ofcourse

ChrisH
  • 1,283
  • 1
  • 9
  • 22
1

Please use the following function for your solution,

 function daycount($day, $startdate, $lastdate, $counter=0)
    {
        if($startdate >= $lastdate)
        {
            return $counter;
        }
        else
        {
            return daycount($day, strtotime("next ".$day, $startdate), ++$counter);
        }
    }

$start_date = "2013-05-01";
$last_date = "2013-08-30";

echo "Tuesday Count - ".daycount("tuesday", strtotime($start_date), strtotime($last_date));
echo "<br/>";
echo "Thursday Count - ".daycount("thursday", strtotime($start_date), strtotime($last_date));
Vinoth Babu
  • 6,724
  • 10
  • 36
  • 55
0

Try with this

$startDate = strtotime($start_date);
$endDate = strtotime($last_date);
while ($startDate < $endDate) {
    echo date('Y-m-d', $startDate ). "\n";
    // Give the condition to find last Tuesday
    $startDate = strtotime( 'next Tuesday', $startDate );
}
GautamD31
  • 28,552
  • 10
  • 64
  • 85
0

With DateTime:

$start_date = "2013-05-01";
$last_date = "2013-08-30";

$start = new DateTime($start_date);
$clone = clone $start;

$start->modify('next thursday');
$thursday=$start->format('Y-m-d');

$clone->modify('next tuesday');
$tuesday=$clone->format('Y-m-d');

echo $thursday; //2013-05-02
echo $tuesday; //2013-05-07

We need to objects because if in interval tuesday is before thursday we will have next tuesday. But you can modify little code to use one object.

Narek
  • 3,813
  • 4
  • 42
  • 58
0

With the help of few php date functions this can be solved easily..

<?php

// Create the from and to date
$start_date = strtotime("2013-05-01");
$last_date  = strtotime("2013-08-30");

// Get the time interval to get the tue and Thurs days
$no_of_days = ($last_date - $start_date) / 86400; //the diff will be in timestamp hence dividing by timestamp for one day = 86400
$get_tue_thu_days = array();

// Loop upto the $no_of_days
for($i = 0; $i < $no_of_days; $i++) {
    $temp = date("D", $start_date);
    if($temp == "Tue" || $temp == "Thu") {
      $get_tue_thu_days[] = date("D/M/Y", $start_date); //formating date in Thu/May/2013 formate.
    }
    $start_date += 86400;
}

print_r($get_tue_thu_days);
Bala Varadarajan
  • 514
  • 1
  • 5
  • 17
-1

if you have a reference date which you know is a tuesday/thursday you can find days which are a multiple of 7 days from your reference date, these days will always be the same day of the week.

smitec
  • 3,049
  • 1
  • 16
  • 12
  • i need a code (PHP) to find the all the dates of tuesday and thurday between those two dates. – yajay May 08 '13 at 12:29