Viewed   211 times

Expected Input:

getDatesFromRange( '2010-10-01', '2010-10-05' );

Expected Output:

Array( '2010-10-01', '2010-10-02', '2010-10-03', '2010-10-04', '2010-10-05' )

 Answers

2

You could also take a look at the DatePeriod class:

$period = new DatePeriod(
     new DateTime('2010-10-01'),
     new DateInterval('P1D'),
     new DateTime('2010-10-05')
);

Which should get you an array with DateTime objects.

To iterate

foreach ($period as $key => $value) {
    //$value->format('Y-m-d')       
}
Friday, November 18, 2022
1

Just try with:

$date1  = '2013-11-15';
$date2  = '2014-02-15';
$output = [];
$time   = strtotime($date1);
$last   = date('m-Y', strtotime($date2));

do {
    $month = date('m-Y', $time);
    $total = date('t', $time);

    $output[] = [
        'month' => $month,
        'total' => $total,
    ];

    $time = strtotime('+1 month', $time);
} while ($month != $last);


var_dump($output);

Output:

array (size=4)
  0 => 
    array (size=2)
      'month' => string '11-2013' (length=7)
      'total' => string '30' (length=2)
  1 => 
    array (size=2)
      'month' => string '12-2013' (length=7)
      'total' => string '31' (length=2)
  2 => 
    array (size=2)
      'month' => string '01-2014' (length=7)
      'total' => string '31' (length=2)
  3 => 
    array (size=2)
      'month' => string '02-2014' (length=7)
      'total' => string '28' (length=2)
Monday, December 5, 2022
 
5

The solution is still very similar to the question you're linking to; try this query:

SELECT * FROM events e
    WHERE `start` <= [RANGE.end]
    AND `end`  >= [RANGE.start]

You'd of course have to replace [RANGE.start] and [RANGE.end] by the first and last date of your range. If e.g. RANGE.start = '2011-04-01' and RANGE.end = '2011-04-30', the above query will give all results which are happening in April '11.

Depending on whether you want to select events which just "touch" the range (meaning they have a common border date, but do not actually overlap) or not, you can replace <=/>= by </>.

Monday, October 10, 2022
 
2

With a little help of a numbers table.

declare @T table
(
  ID int identity primary key,
  FromDate date,
  ToDate date
)

insert into @T values
('2011-11-10', '2011-11-12'),
('2011-12-12', '2011-12-14')

select row_number() over(order by D.Dates) as SN,
       D.Dates
from @T as T
  inner join master..spt_values as N
    on N.number between 0 and datediff(day, T.FromDate, T.ToDate)
    cross apply (select dateadd(day, N.number, T.FromDate)) as D(Dates)
where N.type ='P'

Try on SE Data

Wednesday, October 12, 2022
 
5

This is not possible without a recursive common table expression, which was introduced in SQLite 3.8.3:

WITH RECURSIVE dates(date) AS (
  VALUES('2015-10-03')
  UNION ALL
  SELECT date(date, '+1 day')
  FROM dates
  WHERE date < '2015-11-01'
)
SELECT date FROM dates;
Thursday, September 29, 2022
 
torbik
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :