You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Codeigniter 2.2.6中实现药品6个月到期自动告警

Hey there! Let's tweak your code to get that 6-month drug expiry alert working correctly. Here's how to do it properly:

First, Let's Clean Up & Refactor the Logic

Your original code was set up for passport expiry checks, so we'll adjust variable names to match your drug use case and fix the date comparison logic to target a 6-month window instead of days.

Recommended Approach (Using DateTime for Reliability)

The DateTime class in PHP is far more reliable than strtotime for date calculations, especially when dealing with edge cases like month-end dates (e.g., January 31st + 6 months). Here's the updated code:

// Set your server timezone to match your database's timezone (critical for accurate comparisons!)
// Replace 'UTC' with your actual timezone, e.g., 'Asia/Calcutta' or 'America/New_York'
date_default_timezone_set('UTC');

// Loop through your drug records (renamed variables for clarity)
foreach($drugs as $drug) {
    // Pull the expiry date from your database
    $dbExpiryDate = $drug->expiry_date;
    
    // Create DateTime objects for accurate date math
    $currentDate = new DateTime();
    $sixMonthsFromNow = (new DateTime())->modify('+6 months');
    $drugExpiryDate = DateTime::createFromFormat('Y-m-d', $dbExpiryDate);
    
    // Check if the drug expires within the next 6 months (and hasn't expired yet)
    if ($drugExpiryDate >= $currentDate && $drugExpiryDate <= $sixMonthsFromNow) {
        echo "<div class='alert alert-warning'>Your Drug Is Going Expiry Within 6 Months</div>";
    }
}

Alternative (Using strtotime if You Prefer)

If you'd rather stick with strtotime, this version works too—just note it's less reliable for edge-case dates:

date_default_timezone_set('UTC');

foreach($drugs as $drug) {
    $dbExpiryDate = $drug->expiry_date;
    
    // Convert dates to timestamps for comparison
    $currentTime = time();
    $sixMonthsLater = strtotime('+6 months');
    $drugExpiryTime = strtotime($dbExpiryDate);
    
    // Check the 6-month window
    if ($drugExpiryTime >= $currentTime && $drugExpiryTime <= $sixMonthsLater) {
        echo "Your Drug Is Going Expiry Within 6 Months";
    }
}

Key Notes to Keep in Mind

  • Timezone Consistency: Always set the timezone to match what's stored in your database. If your expiry dates are saved in a different timezone than your server, your comparisons will be off.
  • Date Format: Make sure createFromFormat matches the format of your expiry_date field. If your database uses d/m/Y instead of Y-m-d, update the format string to 'd/m/Y'.
  • Including Expired Drugs: If you want to alert for drugs that have expired in the last 6 months too, remove the $drugExpiryDate >= $currentDate condition from the if statement.
  • User Experience: Wrap the alert in HTML styling (like the Bootstrap alert class in the first example) to make it stand out on the user page.

内容的提问来源于stack exchange,提问作者Iswarya Swaminadhan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:37:55