从PHP数组中找到最接近的下一个日期

时间:2019-02-16 12:01:43

标签: php arrays sorting

我有这个数组,希望找到02/21/2019的下一个最近日期并设置为我页面上的下一个日期,任何帮助或想法都将受到赞赏

 Array
    (
        [0] => 2019-02-17
        [1] => 02/09/2019
        [2] => 02/23/2019
        [3] => 02/18/2019
        [4] => 02/25/2019
        [5] => 03/04/2019
        [6] => 03/11/2019
        [7] => 03/18/2019
        [8] => 03/25/2019
        [9] => 04/01/2019
        [10] => 04/08/2019
    )

4 个答案:

答案 0 :(得分:1)

请参阅此解决方案。希望有帮助

<ion-content padding overflow-scroll="true">
  <ion-grid align-items-center justify-content-center>
    <ion-row align-items-center justify-content-center>
      <ion-col align-items-center justify-content-center class="mycol22" col-12
        *ngFor="let contdetails of this.detailscontact,let i = index">
        <ion-icon name="{{iname[i]}}" class="myion2"></ion-icon>
        <h3 class="myneh31">{{contdetails?.sub_head}}</h3>
        <p class="newmyp2">{{contdetails?.sub_content_first}}</p>
        <p class="newmyp2">{{contdetails?.sub_content_second}}</p>
      </ion-col>
      <ion-col align-items-center justify-content-center class="mycol22" col-12>
        <h3 class="myneh31">Have query? Write to us!</h3>
        <form [formGroup]="submitquery" (ngSubmit)="submityourquery()">
          <ion-list>
            <ion-item class="newitem2">
              <ion-input placeholder="Your Name" type="text" formControlName="name" required></ion-input>
            </ion-item>
            <ion-item class="newitem2">
              <ion-input placeholder="Your Email" type="email" formControlName="email" required></ion-input>
            </ion-item>
            <ion-item class="newitem2">
              <ion-input placeholder="Your Mobile Number" type="number" formControlName="phone" required>
              </ion-input>
            </ion-item>
            <p *ngIf="submitquery.controls['phone'].errors && submitquery.controls['phone'].dirty && submitquery.get('phone').touched"
              style="color: red">
              Mobile Number Must Be 10 Characters.
            </p>
            <ion-item class="newitem2">
              <textarea placeholder="Message" formControlName="message" required></textarea>
            </ion-item>
            <div>
              <button [disabled]="!submitquery.valid" ion-button type="submit" class="newbtn11" color="primary"
                block>Submit</button>
              <button ion-button type="reset" class="newbtn11" color="primary" block>Reset</button>
            </div>
          </ion-list>
        </form>
      </ion-col>
    </ion-row>
  </ion-grid>
</ion-content> 

答案 1 :(得分:0)

将日期转换为Unix时间并比较数字。我没有测试,只是一个主意...

按顺序排列数组元素,然后选择最接近日期(元素)的下一个元素

答案 2 :(得分:0)

这是我用来为程序查找最接近的下一个日期的解决方案,并且可以正常工作。

   $date = '02/21/2019';
   $allDates= array
    (
        '2019-02-17',
        '02/09/2019',
        '02/23/2019',
        '02/18/2019',
        '02/25/2019',
        '03/04/2019',
        '03/11/2019',
        '03/18/2019',
        '03/25/2019',
        '04/01/2019',
         '04/08/2019',
    );    
   function date_sort($a, $b) {
        return strtotime($a) - strtotime($b);
    }
    usort($allDates, "date_sort");
    foreach ($allDates as $count => $dateSingle) {
        if (strtotime($date) < strtotime($dateSingle))  {
            $nextDate = date('m-d', strtotime($dateSingle));
            break;
        }
    }
echo $nextDate;

答案 3 :(得分:0)

我假设您可以修复数组的格式并使之相同

$dates = array
                            (
                                '0'=> "2013-02-18",
                                '1'=> "2013-02-12",
                                '2'=> "2013-02-05",
                                '3'=> "2013-01-29",
                                '4'=> "2013-01-27"
                            );


                        $current = date("Y-m-d");
                        //$count = 0;
                        foreach($dates as $day)
                        {
                            //$interval[$count] = abs(strtotime($date) - strtotime($day));
                            $interval[] = abs(strtotime($current) - strtotime($day));
                            //$count++;
                        }                   
                        asort($interval);
                        $closest = key($interval);                       
                        echo trim($datarray[$closest]);