现有脚本按日、周、月、年循环遍历日期范围?

发布于 2024-09-17 17:09:23 字数 111 浏览 7 评论 0原文

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

如果有的话,我很想找到一个已经存在的。

谢谢!

I'm trying to find an existing helper function that will accept a start and end date and interval (day, week, month, year), and will return an array each day, week month or year in that range.

Would love to find a pre-existing one if it's out there.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海夕 2024-09-24 17:09:23

这是一个粗略的开始:

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;
}

Here's a rough start:

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;
}
标点 2024-09-24 17:09:23

在 PHP 5.3 及更高版本中,您可以使用 DateInterval 类来实现

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

In PHP 5.3 and later you can use DateInterval class for that

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

浮生面具三千个 2024-09-24 17:09:23

我在寻找同样的东西时发现了这个线程。这是我想出的方法,效果很好。

我有一个电子邮件地址数据库和他们订阅电子邮件列表的 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 天或每天或其他时间间隔。

我确信这并不漂亮——我不是最好的程序员——但我不得不把我的解决方案留给那些在我身后的人。

I found this thread while I was looking for this same thing. Here is what I came up with that worked great.

I had a database of email addresses and unix timestamps of when they subscribed to an email list. I wanted to see what the weekly subscribe rate was.

$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;
}

You could change the 604800 number to do intervals of 30 days or 365 days or daily or whatever.

I'm sure this isn't pretty -- I'm not the best coder -- but I had to leave my solution for those that come behind me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文