Unable To use Same Snippet Multiple Times On Same Page

I have the following snippet which, for elections, computes the first Tuesday after the first Monday in May and November. It also allows for subtracting x number of days to computer the end of registration, early voting, etc. I need to use the snippet multiple times on the same page to reflect the dates of various situations, but once I reference the snippet (tried cached and uncached), it errors out and the page can’t be displayed.

Thoughts?


<?php
if (!$days) { $days = 0; }
$election = '';
$election_date = '';
$current_year  = date("Y",time());
$monday_may_string = 'first monday ' . $current_year . '-05';
$monday_november_string = 'first monday ' . $current_year . '-11';
$first_monday_in_may = date('Y-m-d H:i:s', strtotime($monday_may_string));
$first_monday_in_november = date('Y-m-d H:i:s', strtotime($monday_november_string));
$first_tuesday_in_may = strtotime($first_monday_in_may + 864000);
$first_tuesday_in_november = strtotime($first_monday_in_november + 864000);
$right_now = time();

// Primary Election
if ($right_now <= $first_tuesday_in_may + 39600) {
    $election = 'Primary';
    $election_date = first_tuesday($current_year, "May", "F j, Y", $days);
}
elseif ($right_now > $first_tuesday_in_may + 39600 && $right_now <= $first_tuesday_in_november + 39600) {
    $election = 'General';
    $election_date = first_tuesday($current_year, "November", "F j, Y", $days);
}
else {
    $election = 'Primary';
    $current_year++;
    $election_date = first_tuesday($current_year, "May", "F j, Y", $days);
}

echo $election_date;

function first_tuesday($year,$month,$format,$days) {
    $return = '';
    $u = strtotime("first Monday of $month $year");
    $u = ($u + 86400) - ($days *86400);
    $return = $u;
    if (isset($format)) {
        $return = date($format, $u);
    }
    return $return;
}

The problem might be that your first_tuesday function gets declared multiple times when you run the snippet multiple times.

Try something like this:

<?php
if (!$days) { $days = 0; }
$election = '';
$election_date = '';
$current_year  = date("Y",time());
$monday_may_string = 'first monday ' . $current_year . '-05';
$monday_november_string = 'first monday ' . $current_year . '-11';
$first_monday_in_may = date('Y-m-d H:i:s', strtotime($monday_may_string));
$first_monday_in_november = date('Y-m-d H:i:s', strtotime($monday_november_string));
$first_tuesday_in_may = strtotime($first_monday_in_may + 864000);
$first_tuesday_in_november = strtotime($first_monday_in_november + 864000);
$right_now = time();

// Define first_tuesday function if not already defined
if (!function_exists('first_tuesday')) {
    function first_tuesday($year, $month, $format, $days)
    {
        $return = '';
        $u = strtotime("first Monday of $month $year");
        $u = ($u + 86400) - ($days * 86400);
        $return = $u;
        if (isset($format)) {
            $return = date($format, $u);
        }
        return $return;
    }
}

// Primary Election
if ($right_now <= $first_tuesday_in_may + 39600) {
    $election = 'Primary';
    $election_date = first_tuesday($current_year, "May", "F j, Y", $days);
}
elseif ($right_now > $first_tuesday_in_may + 39600 && $right_now <= $first_tuesday_in_november + 39600) {
    $election = 'General';
    $election_date = first_tuesday($current_year, "November", "F j, Y", $days);
}
else {
    $election = 'Primary';
    $current_year++;
    $election_date = first_tuesday($current_year, "May", "F j, Y", $days);
}

echo $election_date;
1 Like

You are amazing! Thank you so much!

1 Like

You could also maybe use toPlaceholder and avoid calling the snippet multiple times, depending on how you’ve got things set up.

1 Like

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.