PHP; auto generate year according to registration date

I’m using s2member with WordPress.

I set up a Fixed EOT(End of Term) for July 31st.

Here’s the coding I’m using..

<?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" ... /]

The code works fine as it is to set the EOT date as July 31, 2013.

But I need the year to auto generate according to the date.

If the user registers before July 31, EOT year should stay the current year.

If the user registers after July 31, EOT year should be set to the following year(2014).

And this will work without having to modify every year.

Read More

Hope this makes sense! Thank you for your help!

EDITED

would this work…

<?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" ... /]

EDIT #2
have this code now..

<?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" ... /]
?>

problem is…

a) FIXED the EOT date comes out to be July 30th.

b) if the user registers AFTER the date, i get an error on the page saying the value needs to be 1 or greater..

“Invalid form configuration. Invalid “tp” attribute. The Trial Period. When provided, must be >= 1.”

still need help..

=============== SOLUTION ================

Figured out a working model! Thanks for everybody’s help!

<?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" ... /]

This structure successfully implements July 31st as the EOT date. When the user registers before July 31st, the current year is set. If the user registers after July 31st, the next year is set.

Related posts

Leave a Reply

3 comments

  1. You can just advance the year if we are after the $fixed_time

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

    UPDATE for new code:

    $now = strtotime("now");
    $mmdd = (int)date("M d");
    echo "mmdd is: $mmdd" . PHP_EOL;
    

    Outputs:

    mmdd is: 0

    That code does not work as intended. Casting a date to an int is a bad idea as you can see if produces 0, which means your if statement will ALWAYS be true;

    My suggestion works fine look at this code that tests before the cutoff and after the cutoff. Your code is in the doStuff function with the strtotime("now") commented out so I can change the date strings:

    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;
    }
    

    Output:

    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
    
  2. If you change this line to read the year dynamically:

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

    that should work.

  3. Assuming that you’ve converted $now into time() format:

    $now = time();
    $term = mktime(0,0,0,7,31);
    $term = ( $now > $term ) ? $term + 31536000 : $term;
    

    31536000 = number of seconds in 1 year

    if you’re doing this function a lot (in a loop?) strtotime is expensive