PHP更改年份时的日期问题

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>

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'));