1

Assume that selected date from Canlender is 02/09/2011. To store weekly date into array from 20/09/2011 is

for($i=0; $i<7; $i++)
{
    $WeeklyDate[] = date("Y-m-d", strtotime(2011-09-02) - 86400*$i);
}

My question is how to store monthly date into array from the selected date.

Many thanks

---Update----------

The final result of monthlyDate should look like the following:

$monthlyDate= array{2011-08-03, 2011-08-04, 2011-08-05, 2011-08-06, 2011-08-07 ....2011-08-31, 2011-09-01, 2011-09-02}
hakre
  • 193,403
  • 52
  • 435
  • 836
Acubi
  • 2,793
  • 11
  • 41
  • 54

2 Answers2

0

Whenever programs are incrementing a date using 86400 there is a risk of unexpected output because of DST.

By using strtotime() with a unit larger than hours (like days, weeks, months, etc.) preventing any DST hiccups. Note: a DateTime object approach can be used but for this case, it is unnecessary overhead.

The following is an adjusted form of a one-liner date range function I developed.

Here is the online demo for this case.

function getDatesFromRange($a,$b,$x=0,$dates=[]){
    while(end($dates)!=$b && $x=array_push($dates,date("Y-m-d",strtotime("$a +$x day"))));
    return $dates;
}
$date='2011-09-02';
$monthlyDate=getDatesFromRange(date("Y-m-d",strtotime("$date -1 month +1 day")),$date);
var_export($monthlyDate);

output as desired/expected:

array (
  0 => '2011-08-03',
  1 => '2011-08-04',
  2 => '2011-08-05',
  3 => '2011-08-06',
  4 => '2011-08-07',
  5 => '2011-08-08',
  6 => '2011-08-09',
  7 => '2011-08-10',
  8 => '2011-08-11',
  9 => '2011-08-12',
  10 => '2011-08-13',
  11 => '2011-08-14',
  12 => '2011-08-15',
  13 => '2011-08-16',
  14 => '2011-08-17',
  15 => '2011-08-18',
  16 => '2011-08-19',
  17 => '2011-08-20',
  18 => '2011-08-21',
  19 => '2011-08-22',
  20 => '2011-08-23',
  21 => '2011-08-24',
  22 => '2011-08-25',
  23 => '2011-08-26',
  24 => '2011-08-27',
  25 => '2011-08-28',
  26 => '2011-08-29',
  27 => '2011-08-30',
  28 => '2011-08-31',
  29 => '2011-09-01',
  30 => '2011-09-02',
)
Community
  • 1
  • 1
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • @Acubi If my answer sufficiently answers your question, please award it the green tick (and possibly an upvote for being helpful). If there is something not quite right, please leave a comment and I'll try to fix it up. – mickmackusa Mar 24 '17 at 03:33
0

First, calculate the number of days in a month using cal_days_in_month and then proceed as you are doing with weeks eg:

$days = cal_days_in_month(CAL_GREGORIAN, 9, 2011);

for($i = 0; $i <= $days; $i++)
{
   $MonthlyDate[] = date("Y-m-d", strtotime(2011-09-02) - 86400*$i);
}

Notice that CAL_GREGORIAN is a built-in constant.

Working Example

Community
  • 1
  • 1
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • The codepad link does not provide the desired output. The code in the answer yields `FATAL ERROR Invalid numeric literal` on the `$MonthlyDate[]` line. Please edit or remove it so SO readers are not confused by it. – mickmackusa Mar 24 '17 at 03:37