douyiji3919 2017-02-23 23:14
浏览 22
已采纳

PHP更改年份时的日期问题

I'm a newb with PHP and code in general, so please forgive any sloppiness...

What I've made here is an expiring link tool so someone can make their links expire past a certain date ($getdate).

If the person clicks the link BEFORE the $getdate then they'll be passed on to the $goodurl.

If the person clicks the link AFTER the $getdate then they'll be passed onto the $expired url.

An example URL would be: https://expiring.click/?d=02/24/2017&g=https://example.com/deal/&e=https://example.com/no-deal/

Hopefully that makes some sense.

Now, this code works with one exception ... when the current year is different than the expiration year.

When that happens, people are always sent to the $goodurl.

So, if my link was set to expire in 02/02/2016 and I clicked on it today (02/23/2017) ... I would see the $goodurl even tho the expiration date is over a year ago.

I can't figure it out to save my life.

Hopefully one of you nice folks can help!

Thanks.

<?php
$getdate = htmlspecialchars($_GET["d"]); // Date Good Through
$sentdate = date("m/d/Y", strtotime($getdate)); // Make sure date is in correct format
$goodurl = htmlspecialchars($_GET["g"]); // Good URL
$expiredurl = htmlspecialchars($_GET["e"]); // Expired URL
$expired = htmlspecialchars($_GET["expired"]); // For Expired = y

// Date Stuff
date_default_timezone_set('America/New_York');
$date = date('m/d/Y');
$datetime = date('m/d/Y h:i:s a', time());
$tomorrow = date('m/d/Y',strtotime($date . "+1 days"));

// Formula Stuff
if ($expired != "y" && !empty($getdate)){
    if ($sentdate < $date){ //expired
        if (empty($expiredurl)) {
            $link = "https://expiring.click/?expired=y";
        } else {
            $link = $expiredurl;
        }
    } else if ($sentdate >= $date){ //in date
        $link = $goodurl;
    }
    header( 'Location: '.$link ) ;
} else if ($expired == "y") {
    echo "Sorry, this link has expired.";
} else {
    ?>

</div>
  • 写回答

1条回答 默认 最新

  • donglie7268 2017-02-23 23:37
    关注

    You have to compare after using strtotime() funtion on both sent date and date.

    $sentdate = date("m/d/Y", strtotime($getdate));

    should be

    $sentdate = strtotime(date("m/d/Y", strtotime($getdate)));
    

    And $date = date('m/d/Y');

    should be

    $date = strtotime(date('m/d/Y'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据