I am in the middle of making a web based system for shows and each show as a different closing date for entries. The closing date is stored in a database. I need to display a export to excel button once the closing date has passed for each show.

I have already done the export to excel button and that works but it's clickable all the time, I need the button to only be clickable once the closing date for each show has passed. I'm thinking I need to check if the closing date in the database has passed compared to todays(current) date.

Not sure how it would work if each show has a different closing date though?

Below is what I came up with but know it's way off

<?php
$sql="SELECT closing_date FROM shows";
$result1 = mysqli_query($mysqli, $sql);
while($info = mysqli_fetch_assoc($result1)){
if(date('Y-m-d') > $info['closing_date'] ){
echo '<input type="submit" name="export_excel" class="btn btn-success float-md-right" value="Export to Excel">';
break;
}
elseif(date("Y-m-d") < $info['closing_date'] ){
echo '<a href="javascript:void(0);"><input type="submit" name="export_excel" class="btn btn-dark float-md-right" value="Export to Excel"></a>';
break;
}
}
?>

Dani AI

Generated

— the goal is to decide per show whether that show’s export control is enabled, not to decide once for the whole loop. Two quick points from the thread: remove the break inside your loop (it stops after the first row) and use a per-row flag so each show row renders its own enabled/disabled button. ’s idea to render a disabled control is fine for UX, but don’t rely on client-side disabling alone. ’s question about storage type matters — DATE vs DATETIME vs TIMESTAMP behave differently in comparisons.

A compact, efficient approach is to let the database tell you which shows are closed and render each row accordingly. For example, select an is_closed column from MySQL then render a button per row:

SELECT id, title, closing_date,
       (closing_date <= NOW()) AS is_closed
FROM shows;

Then render each show row and output an enabled button only when is_closed is true (no break, one button per row). Use prepared statements or PDO when querying and escaping output when printing HTML.

Always enforce the rule on the server side in the export handler as well. Example server-side check (pseudo-PHP):

$stmt = $pdo->prepare('SELECT closing_date FROM shows WHERE id = ?');
$stmt->execute([$showId]);
$closing = new DateTime($stmt->fetchColumn());
if (new DateTime('now') < $closing) { http_response_code(403); exit; }

Notes and troubleshooting: ensure PHP and MySQL use the same timezone, or compare dates in UTC; choose comparisons appropriate to the MySQL type (DATE ignores time-of-day, DATETIME/TIMESTAMP include it). For reference on MySQL date/time types see the manual: MySQL date and time types.

Recommended Answers

All 2 Replies

Use the disabled property:

<?php
$sql = "SELECT closing_date FROM shows";

$result1 = mysqli_query($mysqli, $sql);

while ($info = mysqli_fetch_assoc($result1)) {
    if (date('Y-m-d') > $info['closing_date']) {
        echo '<input type="submit" name="export_excel" class="btn btn-success float-md-right" value="Export to Excel">';
        break;
    } else {
        echo '<input type="submit" name="export_excel" class="btn btn-dark float-md-right" value="Export to Excel" disabled>';
        break;
    }
}
?>

The second if statement is unnecessary, since the export link will be clickable when the view date is after the closing date (not on or before the closing date).

In what format is the closing_date stored in MySQL? Is it stored as a timestamp field? A date field? You might not be able to do a PHP comparison like that depending on the format that MySQL stores the field as.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.