繁体   English   中英

年份更改时的PHP日期问题

[英]PHP Date issue when year changes

我一般来说都是PHP和代码的新手,所以请原谅任何草率的行为...

我在这里所做的是一个过期的链接工具,以便某人可以使他们的链接在特定日期( $getdate )之后过期。

如果此人在$getdate之前单击链接,那么他们将被传递到$goodurl

如果此人在$getdate之后单击链接,那么他们将被传递到$ expired url上。

网址示例为: https : //expiring.click/?d=02/24/2017&g=https : //example.com/deal/&e=https : //example.com/no-deal/

希望这是有道理的。

现在,此代码具有一个例外...当当前年份与到期年份不同时。

发生这种情况时,人们总是被发送到$goodurl

因此,如果我的链接设置为在02/02/2016到期,而我今天(02/23/2017)单击了它, $goodurl即使过期日期超过一年,我也会看到$goodurl

我想不出什么来挽救我的生命。

希望你们中的好人可以为您提供帮助!

谢谢。

 <?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 { ?> 

在发送日期和日期都使用strtotime()函数之后,您必须进行比较。

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

应该

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

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

应该

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM