代码之家  ›  专栏  ›  技术社区  ›  Ian McIntyre Silber

要按天、周、月、年循环显示日期范围的现有脚本?

  •  0
  • Ian McIntyre Silber  · 技术社区  · 14 年前

    我试图找到一个现有的助手函数,它将接受开始和结束日期和间隔(天、周、月、年),并在该范围内每天、周、月或年返回一个数组。

    如果它在外面的话会很乐意找到一个已经存在的。

    谢谢!

    3 回复  |  直到 11 年前
        1
  •  2
  •   Luke Stevenson    14 年前

    这是一个粗略的开始:

    function makeDayArray( $startDate , $endDate ){
     // Just to be sure - feel free to drop these is your sure of the input
      $startDate = strtotime( $startDate );
      $endDate   = strtotime( $endDate );
    
     // New Variables
      $currDate  = $startDate;
      $dayArray  = array();
    
     // Loop until we have the Array
      do{
        $dayArray[] = date( 'Y-m-d' , $currDate );
        $currDate = strtotime( '+1 day' , $currDate );
      } while( $currDate<=$endDate );
    
     // Return the Array
      return $dayArray;
    }
    
        2
  •  1
  •   Mchl    14 年前

    在php 5.3及更高版本中,可以使用dateinterval类

    http://www.php.net/manual/en/class.dateinterval.php

        3
  •  0
  •   Andrew Barber Eric Lafortune    12 年前

    我在找同样的东西的时候发现了这根线。这就是我想到的,效果很好。

    我有一个电子邮件地址数据库,以及他们订阅电子邮件列表时的Unix时间戳。我想看看每周的订阅率是多少。

    $startdate = 1325376000; // Jan 1 2012
    //$startdate = 1293926400; // Jan 1 2011
    $currentstart = $startdate;
    $currentend = $startdate + 604800; // 604800 = 1 week
    
    while($currentend < 1356998400) {
        $sql = "SELECT * FROM the_table WHERE unixdate > $currentstart AND unixdate <= " . ($currentstart + 604800 - 1);
        $result = mysql_query($sql);
        echo date('D Y-m-d', ($currentstart)) . " - " .  date('D Y-m-d', ($currentstart + 604800 - 1)) . ": <strong>" . mysql_num_rows($result) . "</strong><br />";
        $currentstart = $currentend;
        $currentend += 604800;
    }
    

    您可以将604800的数字更改为30天或365天或每天或其他时间间隔。

    我敢肯定这不是很好——我不是最好的编码员——但我必须把我的解决方案留给后面的人。