问题
Created the make time below to display biweekly dates
<?php
$date1 = "07/05/2013";
$date2 = date('M j, Y', strtotime($date1 . " + 14 day"));
$date3 = date('M j, Y', strtotime($date2 . " + 14 day"));
$date4 = date('M j, Y', strtotime($date3 . " + 14 day"));
$date5 = date('M j, Y', strtotime($date4 . " + 14 day"));
$date6 = date('M j, Y', strtotime($date5 . " + 14 day"));
$date7 = date('M j, Y', strtotime($date6 . " + 14 day"));
$date8 = date('M j, Y', strtotime($date7 . " + 14 day"));
$date9 = date('M j, Y', strtotime($date8 . " + 14 day"));
$date10 = date('M j, Y', strtotime($date9 . " + 14 day"));
$date11 = date('M j, Y', strtotime($date10 . " + 14 day"));
$date12 = date('M j, Y', strtotime($date11 . " + 14 day"));
$date13 = date('M j, Y', strtotime($date12 . " + 14 day"));
$date14 = date('M j, Y', strtotime($date13 . " + 14 day"));
$date15 = date('M j, Y', strtotime($date14 . " + 14 day"));
$date16 = date('M j, Y', strtotime($date15 . " + 14 day"));
$date17 = date('M j, Y', strtotime($date16 . " + 14 day"));
$date18 = date('M j, Y', strtotime($date17 . " + 14 day"));
?>
How can I get it to group together by Month? Let say I want to know how many dates land in August, or December. Also if I wanted to get how many dates till end of the year? Helping hand will be greatly appreciated.
回答1:
I wasn't sure exactly what you meant about grouping by months but this will use use an array to declare all the dates and you can change the $limit variable to increase or decrease the amount of dates. change $dates to adjust your original date $left is how many days are left in the array (it triggers after the first day is hit, can be changed to trigger on the last day) and $amount is how many dates are in august
<?php
$limit = 17;
$dates = array("07-05-2013");
for ($i=1; $i<=$limit; $i++){
$dates[$i]=date('d-m-Y', strtotime($dates[$i-1] . "+ 14 days"));
}
foreach (array_keys($dates) as $key){
$value = date('m', strtotime($dates[$key]));
if ($value == "08"){
$amount = $amount + 1;
}
if ($amount == 1){
$left = $limit-$key;
}
}
print_r ($dates);
echo "<br>";
echo $amount . "<br>" . $left;
?>
回答2:
Try this:
<?php
// initiate months
$month_arr = Array();
for ($i=1; $i<=12; $i++){
// no. of dates
$month_arr[$i] = 0;
}
$date_arr = Array();
$date_start = "07/05/2013";
$date_arr[] = date('M j, Y', strtotime($date_start));
for ($i=1; $i<=17; $i++){
$date_temp = date('M j, Y', strtotime($date_arr[$i-1] . " + 14 day"));
$month = date('n', strtotime($date_temp));
$month_arr[$month] += 1;
$date_arr[] = $date_temp;
}
foreach ($month_arr as $k => $v){
echo "<BR>Month: " . $k . ", No. of dates: " . $v;
}
// all dates
echo "<BR>All dates<BR>";
var_dump ($date_arr);
?>
You can extend this logic to group the actual dates by months rather than getting only the count of dates in a month. This is the solution for that:
<?php
// initiate months
$month_arr = Array(
'January' => Array('num_dates'=>0, 'dates'=>Array()) ,
'February' => Array('num_dates'=>0, 'dates'=>Array()),
'March' => Array('num_dates'=>0, 'dates'=>Array()),
'April' => Array('num_dates'=>0, 'dates'=>Array()),
'May' => Array('num_dates'=>0, 'dates'=>Array()),
'June' => Array('num_dates'=>0, 'dates'=>Array()),
'July' => Array('num_dates'=>0, 'dates'=>Array()),
'August' => Array('num_dates'=>0, 'dates'=>Array()),
'September' => Array('num_dates'=>0, 'dates'=>Array()),
'October' => Array('num_dates'=>0, 'dates'=>Array()),
'November' => Array('num_dates'=>0, 'dates'=>Array()),
'December' => Array('num_dates'=>0, 'dates'=>Array())
);
$date_arr = Array();
$date_start = "07/05/2013";
$date_arr[] = date('M j, Y', strtotime($date_start));
for ($i=1; $i<=17; $i++){
$date_temp = date('M j, Y', strtotime($date_arr[$i-1] . " + 14 day"));
$month = date('F', strtotime($date_temp));
$month_arr[$month]['dates'][] = $date_temp;
$month_arr[$month]['num_dates'] += 1;
$date_arr[] = $date_temp;
}
foreach ($month_arr as $k => $v){
if (!empty($v)){
if ($v['num_dates'] != 0){
echo "<BR><BR>Month: " . $k;
echo "<BR>No. of dates: " . $v['num_dates'];
foreach ($v['dates'] as $k1=>$v1){
echo "<BR>" . $v1;
}
}
}
}
?>
At this point, $month_arr should have everything you need.
回答3:
just add an if statement and increment through all 12 months ... if the months are the same
$n=1;
$month=array();//dumpyour dates in an array
$date=array()
while($k<30){
if (date('n', strtotime($date[$k]))==date('n', strtotime($date[$k-1])){
echo $date[$k++];
$m = date('n', strtotime($date[$k]));
$month[$m]=++$n;
}
else{
.....
}
}
回答4:
As I keep re-iterating on SO, PHP's DateTime classes make most datetime operations trivial.
Your question is not 100% clear, but from your comments it seems you want an array of timestamps, 14 days apart sectioned by month. I don't know if you considered that the months may cross year bondaries, but I took that into consideration in my answer.
The code below should do what you want:-
$start = \DateTime::createFromFormat('d/m/Y', '07/05/2013');
$interval = new \DateInterval('P14D');
$periods = new \DatePeriod($start, $interval, 26);
$dates = array();
foreach($periods as $day){
/** @var \DateTime $day */
$dates[$day->format('Y')][$day->format('M')][] = $day->getTimestamp();
}
var_dump($dates);
Output:-
array (size=2)
2013 =>
array (size=8)
'May' =>
array (size=2)
0 => int 1367936286
1 => int 1369145886
'Jun' =>
array (size=2)
0 => int 1370355486
1 => int 1371565086
'Jul' =>
array (size=3)
0 => int 1372774686
1 => int 1373984286
2 => int 1375193886
'Aug' =>
array (size=2)
0 => int 1376403486
1 => int 1377613086
'Sep' =>
array (size=2)
0 => int 1378822686
1 => int 1380032286
'Oct' =>
array (size=2)
0 => int 1381241886
1 => int 1382451486
'Nov' =>
array (size=2)
0 => int 1383664686
1 => int 1384874286
'Dec' =>
array (size=3)
0 => int 1386083886
1 => int 1387293486
2 => int 1388503086
2014 =>
array (size=5)
'Jan' =>
array (size=2)
0 => int 1389712686
1 => int 1390922286
'Feb' =>
array (size=2)
0 => int 1392131886
1 => int 1393341486
'Mar' =>
array (size=2)
0 => int 1394551086
1 => int 1395760686
'Apr' =>
array (size=2)
0 => int 1396966686
1 => int 1398176286
'May' =>
array (size=1)
0 => int 1399385886
I would recommend that, instead of an array of timestamps, you have an array of DateTime objects instead, then you can manipulate each one as you wish. In which case, the code would look like this:-
$start = \DateTime::createFromFormat('d/m/Y', '07/05/2013');
$interval = new \DateInterval('P14D');
$periods = new \DatePeriod($start, $interval, 26);
$dates = array();
foreach($periods as $day){
/** @var \DateTime $day */
$dates[$day->format('Y')][$day->format('M')][] = $day;
}
来源:https://stackoverflow.com/questions/17731572/grouping-dates-into-months-in-php