dtz46697 2017-01-05 11:03
浏览 22
已采纳

根据PHP中的日期隐藏链接

I am displaying a number of dates using PHP and I need to hide them when a certain date has expired.

I am using an IF statement to run this but it doesn't seem to be working.

Any suggestions would be great

<?PHP if('09-19-2016'<DATE('m-d-Y') || $_SESSION['role'] == 'Administrator') echo('<li><a href="?page=itRooms&weekId=2">Week 2 - W/C 12/09/2016</li></a>');?>
  • 写回答

2条回答 默认 最新

  • duankang5285 2017-01-05 11:12
    关注

    When you're doing

    '09-19-2016' < date('m-d-Y')
    

    You're ending up comparing two strings, these can't be evaluated as "greater than" or "less than". You'll need to convert it to timestamps or use DateTime objects to do it. Also, the date format isn't correct.

    <?php 
    $date_string = "09/19/2016";
    
    // Using objects 
    $current_date = new DateTime();
    $your_date    = new DateTime($date_string);
    if ($your_date < $current_date || $_SESSION['role'] == 'Administrator') 
        echo'<li><a href="?page=itRooms&weekId=2">Week 2 - W/C 12/09/2016</li></a>';
    
    // Using timestamps
    if (strtotime($date_string) < time() || $_SESSION['role'] == 'Administrator') 
        echo'<li><a href="?page=itRooms&weekId=2">Week 2 - W/C 12/09/2016</li></a>';
    

    Choose either one of the above - both will work, although I find objects easier to work with.


    From your comments,

    hide the date if the date has passed

    Note that when using the less than operator <, doing $date < $now will evaluate to true if the date is in the past, and hide the content if the date is in the future. If you desire the opposite behavior, you just use the greater than operator <.

    Here's a live demo: https://3v4l.org/N74G2

    References

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式