创建下面的制作时间以显示每两周一次的日期
<?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"));
?>
如何按月将它组合在一起?假设我想知道8月或12月有多少日期。如果我想知道到今年年底有多少日期?非常感谢助手。
答案 0 :(得分:1)
试试这个:
<?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);
?>
您可以扩展此逻辑,将实际日期按月分组,而不是仅获取一个月中的日期计数。这是解决方案:
<?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;
}
}
}
}
?>
此时,$ month_arr应该拥有您需要的一切。
答案 1 :(得分:1)
我不确定你对几个月的分组意味着什么 但是这将使用数组来声明所有日期,您可以更改$ limit变量来增加或减少日期量。 更改$ date以调整您的原始日期 $ left是数组中剩余的天数(它在第一天被触发后触发,可以在最后一天更改为触发) 和金额是8月份的日期数量
<?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 :(得分:0)
只需添加if语句并在所有12个月内增加...如果月份相同
$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{
.....
}
}
答案 3 :(得分:0)
当我继续重复SO时,PHP的DateTime classes使大多数日期时间操作变得微不足道。
您的问题并非100%明确,但是根据您的评论,您似乎需要一系列时间戳,按月划分14天。我不知道你是否认为这些月份可能会跨越年度债券,但我在答案中考虑了这一点。
下面的代码应该做你想要的: -
$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);
输出: -
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
我建议您使用DateTime对象数组代替时间戳数组,然后根据需要操作每个对象。在这种情况下,代码如下所示: -
$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;
}