【问题标题】:making php functions to count specific day使php函数计算特定日期
【发布时间】:2016-01-11 08:31:15
【问题描述】:

我有一个计算一年中有多少天的函数,我通过将$week 变量更改为:

Monday - 6:Saturday,但是我放7就不行了:Sunday

谁能帮忙。我是否缺少任何逻辑?

$year = 2016;
$newyear = $year;
$week = 0;
$day = 0;
$mo = 1;
$days = array();
$i = 1;

        while ($week != 7) { // here is where I change the 1-7 for days
          $day++;
          $week = date("w", mktime(0, 0, 0, $mo,$day, $year));
        }

        array_push($days,date("r", mktime(0, 0, 0, $mo,$day, $year)));
        while ($newyear == $year) {
          $x =  strtotime(date("r", mktime(0, 0, 0, $mo,$day, $year)) . "+" . $i . " week");
          $i++;
          if ($year == date("Y",$x)) {
            array_push($days,date("r", $x));
          }
          $newyear = date("Y",$x);
        }

        print count($days);

感谢您的帮助欢​​呼!并且可以立即计算 2 年的总天数,例如:

我有一个日期是 2016 年 1 月 11 日是星期一,我想知道从 2016 年 1 月 11 日到 2018 年 1 月 11 日有多少天,有多少个星期一。

谢谢!

【问题讨论】:

标签: php date leap-year


【解决方案1】:

使用 DateTime/DateInterval 函数:

$datetime1 = new DateTime('2018-01-11');
$datetime2 = new DateTime('2016-01-11');
$interval = $datetime1->diff($datetime2);
echo floor($interval->format('%a days')/7); // 104

或者使用 strtotime...

您也可以这样做:

$startDate = strtotime('2016-01-11');
$endDate = strtotime('2018-01-11');
$totalWeeks = (($endDate - $startDate)/86400)/7;
echo floor($totalWeeks); // rounds to 104

在这里阅读更多:

DateDiff - http://php.net/manual/en/datetime.diff.php

DateInterval::format - http://php.net/manual/en/dateinterval.format.php

更新...如何轻松地“调试”这一天计数:

<?php

$startDate = strtotime('2016-01-11');
$endDate = strtotime('2018-01-11');
$currentDate = $startDate;

$count = 0;
while ($currentDate <= $endDate) {
    echo date('r', $currentDate) . "\n";
    $currentDate = strtotime('+1 week', $currentDate);
    if ($currentDate<=$endDate) {
        $count++;
    }
}

echo $count . "\n";

【讨论】:

  • 我只需要知道具体的一天,例如在这 2 年的范围内有多少个星期一...
  • 看看我的新答案,看看它是否回答了你的问题,如果对你有用,请告诉我
  • 您在寻找星期一之间的计数吗?还是要包括第一个星期一和最后一个星期一?
  • 嗨克莱顿!是的,它会在周日工作,但如果我想知道周一呢?还是星期二?是否有任何选项可以将值更改为 1 然后它会告诉我有多少个星期一,或者 2 然后它会告诉我有星期二?谢谢!
  • 我正在尝试构建一个小函数,所以当我输入日期时,假设日期是星期一,然后计算 2 年内有多少个星期一,如果日期是星期五,那么如何2年内有很多个星期五,类似的事情
【解决方案2】:
$date_1 =  strtotime("2016-01-11");
$date_2 = strtotime("2018-01-11");
$datediff = $date_2 - $date_1;
echo floor($datediff/(60*60*24));

修改:

您可以在两个日期之间找到一周中的任何一天。只需更改 $days[0] 的值;

星期一:

<?php
$date_1 = $from = strtotime('2016-01-11');
$date_2 = strtotime('2018-01-11');
$days = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
$count = 0;
while ($date_1 < $date_2) {
  if(date('l', $date_1) == $days[0]);
  {
    $count++;   
  }
  $date_1 += 7 * 24 * 3600;
}
echo "From : ".date('Y-m-d',$from)."  To : ".date('Y-m-d',$date_2)."  has $count  $days[0]";
?>

输出:

From : 2016-01-11 To : 2018-01-11 has 105 Monday

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    相关资源
    最近更新 更多