PHP;根据注册日期自动生成年份

PHP;根据注册日期自动生成年份,php,wordpress,date,fixed,Php,Wordpress,Date,Fixed,我将s2member与WordPress一起使用。 我为7月31日设定了固定EOT(期末)。 以下是我正在使用的编码 <?php $now = strtotime("now"); $fixed_time = strtotime("31 July 2013"); $days_until_fixed_time = round(($fixed_time - $now) / ($seconds_in_one_day = $86400)); ?> [s2member-Pro-PayPal-F


我将s2member与WordPress一起使用。
我为7月31日设定了固定EOT(期末)。
以下是我正在使用的编码

<?php
$now = strtotime("now");
$fixed_time = strtotime("31 July 2013");
$days_until_fixed_time = round(($fixed_time - $now) / ($seconds_in_one_day = $86400));
?>

[s2member-Pro-PayPal-Form... tp="<?php echo $days_until_fixed_time; ?>" tt="D" ... /]

[s2member Pro PayPal表单…tp=”“tt=“D”../]
该规范将EOT日期设置为2013年7月31日,因此运行良好。
但我需要根据日期自动生成年份。
如果用户在7月31日之前注册,则EOT年度应保持当前年度。
如果用户在7月31日之后注册,则EOT年应设置为下一年(2014年)。
这将在不必每年修改的情况下工作

希望这有意义!谢谢你的帮助

已编辑
这是否有效

<?php
$now = strtotime("now");
$fixed_time = strtotime("31 July " . date("Y"));

if($now > $fixed_time)
$fixed_time = strtotime("+1 year", $fixed_time);

$days_until_fixed_time = round(($fixed_time - $now) / ($seconds_in_one_day = $86400));
?>

[s2member-Pro-PayPal-Form... tp="<?php echo $days_until_fixed_time; ?>" tt="D" ... /]

[s2member Pro PayPal表单…tp=”“tt=“D”../]
编辑#2 请立即输入此代码

<?php
$now = strtotime("now");
$mmdd = (int)date("M d");
$yyyy = (int)date("Y");
if ($mmdd <= 731)
$fixed_time = strtotime("31 July " . $yyyy);
else
$fixed_time = strtotime("31 July " . ($yyyy+1));

$days_until_fixed_time = round(($fixed_time - $now) / ($seconds_in_one_day = "86400"));

[s2member-Pro-PayPal-Form... tp="<?php echo $days_until_fixed_time; ?>" tt="D" ... /]
?>

问题是。。。
a)确定EOT日期为7月30日。
b)如果用户在日期之后注册,我会在页面上看到一个错误,指出该值需要为1或更大。。
无效的表单配置。无效的“tp”属性。提供的试用期必须大于等于1
仍然需要帮助。。

===================解决方案================
找到了一个工作模式!谢谢大家的帮助

<?php
$now = strtotime("now");
$fixed_time = strtotime("31 July " . date("Y"));

if($now > $fixed_time)
    $fixed_time = strtotime("+1 year", $fixed_time);

$days_until_fixed_time = round(($fixed_time - $now) / (86400));
?>

[s2member-Pro-PayPal-Form... tp="<?php echo $days_until_fixed_time; ?>" tt="D" ... /]

[s2member Pro PayPal表单…tp=”“tt=“D”../]



该结构成功地将7月31日作为EOT日期实施。当用户在7月31日之前注册时,设置当前年份。如果用户在7月31日之后注册,则设置下一年。

如果将此行更改为动态读取年份:

$mmdd = (int)date("M d");
$yyyy = (int)date("Y");
if ($mmdd <= 731)
  $fixed_time = strtotime("31 July " . $yyyy);
else
  $fixed_time = strtotime("31 July " . ($yyyy+1));
$mmdd=(int)日期(“md”);
$yyyy=(整数)日期(“Y”);

如果($mmdd如果我们在
$fixed\u时间之后

if($now > $fixed_time)
    $fixed_time = strtotime("+1 year", $fixed_time);
更新新代码:

产出:

mmdd为:0

该代码没有按预期工作。将日期强制转换为int是个坏主意,因为可以看到if生成0,这意味着if语句将始终为true

我的建议很好,看看这段代码,它在截止之前和截止之后进行测试。您的代码位于doStuff函数中,并且注释掉了
strottime(“now”)
,因此我可以更改日期字符串:

echo "Checking for before cutoff date: " . PHP_EOL;
doStuff("30 July 2013"); // Before the cutoff
echo PHP_EOL . "Checking for after cutoff: " . PHP_EOL;
doStuff("1 August 2013"); // After the cutoff

function doStuff($datestr){
    $now = strtotime($datestr);
    //$now = strtotime("now");
    $fixed_time = strtotime("31 July " . date("Y"));

    echo "Fixed time before change is: " . date("m-d-Y", $fixed_time) . PHP_EOL;
    if($now > $fixed_time)
        $fixed_time = strtotime("+1 year", $fixed_time);

    echo "Fixed time is: " . date("m-d-Y", $fixed_time) . PHP_EOL;

    $days_until_fixed_time = round(($fixed_time - $now) / (86400));

    echo "Days until: $days_until_fixed_time" . PHP_EOL;
}
输出:

Checking for before cutoff date: 
    Fixed time before change is: 07-31-2013
    Fixed time is: 07-31-2013
    Days until: 1

Checking for after cutoff: 
    Fixed time before change is: 07-31-2013
    Fixed time is: 07-31-2014
    Days until: 364

假设您已将$now转换为time()格式:

31536000=一年中的秒数


如果你经常做这个函数(在循环中?)STROTIME是昂贵的

这适用于7月31日之前注册的用户,但是,如果用户在7月31日之后注册…这不会仍然将日期设置为本年,而不是下一年吗?啊,对不起,没有注意到。根据要求编辑了答案。$now将在用户注册时生效,是的。无论用户何时注册,结束期限都是是7月31日。这取决于它是在7月31日之前还是之后,以便系统知道何时设置“年份”。新的更改在我看来很好。编辑后的代码不起作用。没有显示任何内容,不会显示PayPal表单。有什么想法吗?有什么问题吗?编辑后的代码的问题是“一天中的秒数”…修复了它,但仍然存在与以前相同的问题我目前正在使用最新的更新。另一个问题是如何显示此信息。我不是wordpress专家,但我不认为像[s2member Pro From..]这样的标签在wordpress页面之外工作。因此,如果您试图将其显示为HTMl,我怀疑它是否会工作。这只是猜测,因为我不太了解wordpress“循环中”是什么意思?此函数仅在用户注册会员时调用。在一天结束时,仅在需要时使用strotime,因为它更昂贵韩只是把两个数字加在一起。如果只在用户点击按钮提交注册表时使用它,这有什么关系或有多大区别吗?(一年只有100-200次…)没有。我注意到在进行大型迭代时有很大的区别,这就是我提出来的原因,但一次调用不会有多大区别。
Checking for before cutoff date: 
    Fixed time before change is: 07-31-2013
    Fixed time is: 07-31-2013
    Days until: 1

Checking for after cutoff: 
    Fixed time before change is: 07-31-2013
    Fixed time is: 07-31-2014
    Days until: 364
$now = time();
$term = mktime(0,0,0,7,31);
$term = ( $now > $term ) ? $term + 31536000 : $term;