【问题标题】:Timestamps only comparing to days and not month/year in Laravel时间戳仅与 Laravel 中的天数而不是月/年比较
【发布时间】:2020-11-22 22:58:23
【问题描述】:

我试图在我的表格中设置一个条件,以便在 updated_at 时间 + 几分钟间隔(作为一种限制)小于当前时间时出现提交按钮,这存储在变量 $ updated_at 时间加上 $time_gap 限制并转换为日期格式进行比较的时间,但只比较与 $currenttime 的天数,不计算月或年,因此取 7 月 31 日 > 8 月 1 日。 时间变量的代码在这里,我首先将时间戳转换为 strtotime,这样我就可以添加更多时间($tmt 是额外时间,$tmot 是 update_at + $tmt),以便稍后与 $currenttime 进行比较。

                    $timestamps = userData::select('updated_at')->where('username',$username)->get();
                    foreach ($timestamps as $timestamp){
                        $timestamp = $timestamp->updated_at; }
                    $timestamp = strtotime($timestamp);
                    $currentstamp = now();
                    $currentstamp = strtotime($currentstamp);
                    $currenttime = date("Y/m/d H:i:s", $currentstamp);
                    $timegaps = division::select('time_gap')->where('id',$userdiv)->get();
                    foreach ($timegaps as $timegap){
                        $timegap = $timegap->time_gap; }
                    $tmt = $timegap * 60 * 1; $tmot = $timestamp + $tmt;  $time= date("Y/m/d H:i:s", $tmot);

这是PHP中的比较代码:

                      if ($currenttime < $time){
                        }
                      elseif($currenttime >= $time){
                        if ($nextrankdiv==$rankdiv){ ?><tr>
                            
                            <div class="modal fade" id="modal<?php echo $row['id'] ;?>" role="dialog">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h4 class="modal-title">Promotion</h4>
                                            <button type="btn btn-primary" class="close" data-dismiss="modal">X</button>
                                        </div>
                                        <div class="modal-body"> Do you want to promote the following user?
                                            <?php echo '<img src="https://www.habbo.com/habbo-imaging/avatarimage?user='.$username.'&direction=4&head_direction=3&gesture=sml&size=2" />' ; ?>
                                            <form method="post" action="{{route('promote.update',['id' => $id ])}}">
                                                {{csrf_field()}}

                                                <input type="hidden" name="_method" value="PUT" />
                                                <input type="hidden" name="rank" class="'form-control" value="{{$rankid}}" readonly />
                                                <input type="hidden" name="oldrank" class="'form-control" value="{{$oldrankid}}" readonly />
                                                <input type="hidden" name="division" class="'form-control" value="{{$nextrankdiv}}" readonly />
                                                <input type="hidden" name="targetname" class="'form-control" value="{{$targetname}}" readonly />
                                                <input type="hidden" name="id" class="'form-control" value="{{$id}}" readonly />
                                                <input type="submit" class="btn btn-primary" value="Submit" />
                                            </form>
                                            <td align="center"> &nbsp;&nbsp;

                                                <button class="btn btn-primary" data-toggle="modal" data-target="#modal<?php echo $row['id'] ;?>"> Submit </button>
                                                &nbsp;      &nbsp;
                                        </div>
                                    </div>
                                </div>
                            </div>
                            </td>  <?php
                        }
                        elseif ($nextrankdiv>$rankdiv){
                        switch ($train) {
                        case 0: ?><tr>
                          
                            <div class="modal fade" id="modal<?php echo $row['id'] ;?>" role="dialog">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h4 class="modal-title">Promotion</h4>
                                            <button type="btn btn-primary" class="close" data-dismiss="modal">X</button>
                                        </div>
                                        <div class="modal-body"> Do you want to promote the following user?
                                            <?php echo '<img src="https://www.habbo.com/habbo-imaging/avatarimage?user='.$username.'&direction=4&head_direction=3&gesture=sml&size=2" />' ; ?>
                                            <form method="post" action="{{route('promote.update',['id' => $id ])}}">
                                                {{csrf_field()}}

                                                <input type="hidden" name="_method" value="PUT" />
                                                <input type="hidden" name="rank" class="'form-control" value="{{$rankid}}" readonly />
                                                <input type="hidden" name="oldrank" class="'form-control" value="{{$oldrankid}}" readonly />
                                                <input type="hidden" name="division" class="'form-control" value="{{$nextrankdiv}}" readonly />
                                                <input type="hidden" name="targetname" class="'form-control" value="{{$targetname}}" readonly />
                                                <input type="hidden" name="id" class="'form-control" value="{{$id}}" readonly />
                                                <input type="submit" class="btn btn-warning" value="Submit Div" />
                                            </form>
                                            <td align="center"> &nbsp;&nbsp;

                                                <button class="btn btn-warning" data-toggle="modal" data-target="#modal<?php echo $row['id'] ;?>"> Submit </button>
                                                &nbsp;      &nbsp;
                                        </div>
                                    </div>
                                </div>
                            </div>
                            </td>  <?php
                        break;
                        case 1:               ?><tr>
                            
                            <td>&nbsp; &nbsp; <form method="post" action="{{route('promote.update',['id' => $id ])}}">
                                    {{csrf_field()}}
                                    <input type="hidden" name="_method" value="PUT" />
                                    <input type="hidden" name="rank" class="'form-control" value="{{$rankid}}" readonly />
                                    <input type="hidden" name="oldrank" class="'form-control" value="{{$oldrankid}}" readonly />
                                    <input type="hidden" name="division" class="'form-control" value="{{$nextrankdiv}}" readonly />
                                    <input type="hidden" name="targetname" class="'form-control" value="{{$targetname}}" readonly />
                                    <input type="hidden" name="id" class="'form-control" value="{{$id}}" readonly />
                                    <input type="submit" class="btn btn-warning" value="Submit" />
                                </form> &nbsp;  </td><?php
                            break;
                            }                ?>
                            <?php    }
                            }
                            }
                            } ?>
                        </tr>
                    @endforeach

比较只针对天小时/分钟/秒进行,比较时不检查月份和年份,我尝试使用 switch case 而不是 if elseif,但问题仍然存在。

【问题讨论】:

    标签: php laravel timestamp


    【解决方案1】:

    通过将最终日期再次转换为 strtotime 来修复 所以对于上面的代码:

                        $currenttime = date("Y/m/d H:i:s", $currentstamp);
                        $timegaps = division::select('time_gap')->where('id',$userdiv)->get();
                        foreach ($timegaps as $timegap){
                        $timegap = $timegap->time_gap; }
                        $tmt = $timegap * 60 * 1; $tmot = $timestamp + $tmt;  $time= date("Y/m/d H:i:s", $tmot);
                        $currenttime = strtotime($currenttime);
                        $time = strtotime($time)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-13
      • 1970-01-01
      • 2015-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-02
      相关资源
      最近更新 更多