【问题标题】:Calculating Summer/Winter Solstice in PHP?在 PHP 中计算夏/冬至?
【发布时间】:2014-07-21 14:54:58
【问题描述】:

PHP 允许我快速检查特定纬度和经度的任何一天的 sunrisesunset 时间。

有没有一种简单的方法来计算哪一天是冬至?我的意思是 - 在我的特定位置,哪一天的日照时间最多,哪一天的日照时间最少?

【问题讨论】:

    标签: php datetime time astronomy


    【解决方案1】:

    以上代码使用的函数将在 PHP 8.1 中被贬值,并且根据系统配置,可能会导致内存耗尽。我冒昧地重写了该函数,使其与 PHP 8 兼容。

    我们确实知道,至日发生在 6 月 20 日至 23 日和 12 月之间。考虑到这一点,我们可以在这两个特定的月份和这四个特定的日子里归零。

    function solstice() {
        $delta = $dates = array();
        $year = date('Y', time());
        $months = array(6,12);
        $days = array(19,20,21,22,23,24);
        $latitude = 31.47;
        $longitude = 35.13;
        $dateFormat = 'l, d F Y';
        
        foreach ($months AS $month) {
            foreach ($days AS $day) {
                $date = sprintf('%d-%02d-%d', $year, $month, $day);
                $solar = date_sun_info(strtotime($date), $latitude, $longitude);
                $delta[] = ($solar['sunset'] - $solar['sunrise']);
                $dates[] = $date;
                unset($solar);
            }
        }
        
        print('<pre>');
        print_r($delta);
        print_r($dates);
        print('</pre>');
        $shortest_key = array_search(min($delta), $delta);
        $longest_key = array_search(max($delta), $delta);
        
        printf(_('The longest day is: %s<br />'), date($dateFormat, strtotime($dates[$longest_key])));
        printf(_('The shortest day is: %s'), date($dateFormat, strtotime($dates[$shortest_key])));
    }
    

    感谢@MorKadosh 提供基本理念。

    【讨论】:

      【解决方案2】:

      我不会称之为“简单”,但我考虑过计算每天日出和日落之间的时间差,然后将这些数据存储在一个数组中,最后找到最小值/最大值。 我做的东西很快,希望对你有用:

      (我使用随机的长/纬度)

      function solstice() {
      
          // Set timezone
          date_default_timezone_set('UTC');
          $date='2014/01/01';
      
          $end_date='2014/12/31';
          $i = 0;
          //loop through the year
          while(strtotime($date)<=strtotime($end_date)) { 
              $sunrise=date_sunrise(strtotime($date),SUNFUNCS_RET_DOUBLE,31.47,35.13,90,3);
              $sunset=date_sunset(strtotime($date),SUNFUNCS_RET_DOUBLE,31.47,35.13,90,3);
              //calculate time difference
              $delta = $sunset-$sunrise;
              //store the time difference
              $delta_array[$i] = $delta;
              //store the date
              $dates_array[$i] = $date;
              $i++;
              //next day
              $date=date("Y-m-d",strtotime("+1 day",strtotime($date)));
          }
      
          $shortest_key = array_search(min($delta_array), $delta_array);
          $longest_key = array_search(max($delta_array), $delta_array);
      
          echo "The longest day is:".$dates_array[$longest_key]. "<br />";
          echo "The shortest day is:".$dates_array[$shortest_key]. "<br />";
      }
      

      【讨论】:

      • 谢谢!你的代码是我能找到的唯一计算冬/夏至的 PHP 代码。我在自己的项目中对代码进行了一些更改:1) 将日期范围限制为 3-4 天而不是所有 365 天,2) 删除 lat/long/dst 并让默认值生效。
      猜你喜欢
      • 2012-07-04
      • 1970-01-01
      • 2016-08-02
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 2010-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多