Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP创建最近30天的数组_Php_Arrays_Date - Fatal编程技术网

使用PHP创建最近30天的数组

使用PHP创建最近30天的数组,php,arrays,date,Php,Arrays,Date,我正试图从今天开始创建一个数组,并回顾过去30天使用PHP的情况,但我遇到了麻烦。我可以估计,但考虑到上个月的天数等,我不知道一个好的方法。有人有好的解决方案吗?我无法接近,但我需要确保它100%准确。试试这个: <?php $d = array(); for($i = 0; $i < 30; $i++) $d[] = date("d", strtotime('-'. $i .' days')); ?> 您可以使用时间来控制天数: for ($i = 0;

我正试图从今天开始创建一个数组,并回顾过去30天使用PHP的情况,但我遇到了麻烦。我可以估计,但考虑到上个月的天数等,我不知道一个好的方法。有人有好的解决方案吗?我无法接近,但我需要确保它100%准确。

试试这个:

<?php    
$d = array();
for($i = 0; $i < 30; $i++) 
    $d[] = date("d", strtotime('-'. $i .' days'));
?>

您可以使用时间来控制天数:

for ($i = 0; $i < 30; $i++)
{
    $timestamp = time();
    $tm = 86400 * $i; // 60 * 60 * 24 = 86400 = 1 day in seconds
    $tm = $timestamp - $tm;

    $the_date = date("m/d/Y", $tm);
}
($i=0;$i<30;$i++)的

{
$timestamp=time();
$tm=86400*$i;//60*60*24=86400=1天(秒)
$tm=$timestamp-$tm;
$the_date=日期(“m/d/Y”,$tm);
}
现在,在for循环中,您可以将$the_date变量用于任何您想要的用途。:-)

$d=array();
对于($i=0;$i<30;$i++)
数组_unshift($d,strotime('-'.$i.'days'));

对于那些想要显示过去X天销售额的人,
,这对我有用

  $sales = Sale::find_all();//the sales object or array

  for($i=0; $i<7; $i++){
    $sale_sum = 0;  //sum of sale initial
    if($i==0){ 
    $day = strtotime("today"); 
  } else {
   $day = strtotime("$i days ago");
  }
  $thisDayInWords = strftime("%A", $day);

  foreach($sales as $sale){
    $date = strtotime($sale->date_of_sale)); //May 30th 2018 10:00:00 AM
    $dateInWords = strftime("%A", $date);

    if($dateInWords == $thisDayInWords){
        $sale_sum += $sale->total_sale;//add only sales of this date... or whatever
    } 
  } 
  //display the results of each day's sale
  echo $thisDayInWords."-".$sale_sum; ?> 

 } 
$sales=Sale::find_all()//销售对象或数组
对于($i=0;$idate_of_sale))//2018年5月30日上午10:00:00
$dateInWords=strftime(“%A”,$date);
如果($dateInWords==$thisDayInWords){
$sale\u sum+=$sale->total\u sale;//仅添加此日期的销售额…或其他任何内容
} 
} 
//显示每天的销售结果
echo$thisdays大写。“-”$sale\u sum;?>
} 
在你生气之前:
我把这个答案放在这里是为了帮助那个问题的指导者。无法回答:(

这里是advance最新的代码片段

$today     = new DateTime(); // today
$begin     = $today->sub(new DateInterval('P30D')); //created 30 days interval back
$end       = new DateTime();
$end       = $end->modify('+1 day'); // interval generates upto last day
$interval  = new DateInterval('P1D'); // 1d interval range
$daterange = new DatePeriod($begin, $interval, $end); // it always runs forwards in date
foreach ($daterange as $date) { // date object
    $d[] = $date->format("Y-m-d"); // your date
}
print_r($d);
工作


官方。

Pedrin,注意这种使用时间的方法。我曾经这样做过,然后注意到它在涉及日光节约时间的特定时间无法正确计算。相反,我建议使用strotime函数。-Mattyou在I++之前缺少$a……感谢有用的循环:)我想你的意思是将日期分配给$d[I],而不是$d[]。对于那些不知道的人,
$d[]
在这里做的是将结果添加到数组的末尾。对于真正的新人,请注意,
$d
在答案中的输出只是日期,并且包括可以执行此操作的月份和年份:
“d-m-y”
。请查看php的
date()
函数以了解更多信息。有关此解决方案的工作原理以及您认为它是一个好解决方案的原因的一些解释将是本文的一个改进。
$today     = new DateTime(); // today
$begin     = $today->sub(new DateInterval('P30D')); //created 30 days interval back
$end       = new DateTime();
$end       = $end->modify('+1 day'); // interval generates upto last day
$interval  = new DateInterval('P1D'); // 1d interval range
$daterange = new DatePeriod($begin, $interval, $end); // it always runs forwards in date
foreach ($daterange as $date) { // date object
    $d[] = $date->format("Y-m-d"); // your date
}
print_r($d);