Jump to content

[SOLVED] Expression to show date status


hostfreak

Recommended Posts

Hey, I'm storing a date in a database. The date will represent when something expires. I can show when it's expired or current, but I am unsure how to give a notice when it will be expiring within x days up until expiring. Here is what I have:

<?php

    //Dates
    $current = date('Y-m-d');
    $expires = '2007-11-07';
    $notice  = date('Y-m-d', mktime($expires) - 5 * 24 * 60 * 60); //try an interval of 5 days
    
    //Date Status
    if ($current >= $expires)
    {
        echo 'Expired';
    }
    else if () //this is where I am unsure
    {
        echo 'Expiring within x days';
    }
    else
    {
        echo 'Current';
    }

?>

 

By the way, I know I can do this via mysql, but want to know they way via php. Thanks in advanced.

Link to comment
https://forums.phpfreaks.com/topic/75799-solved-expression-to-show-date-status/
Share on other sites

I don't quite think that is what I want. I don't need it to list exactly how many days until the expired date, just inform them if it is within x days of expiring. Although, that could be nice. However for the sake of simplicity, just show within the x days. So say within 32 days; it could be 32 or even 10, but it would still say "Within 32 days". Unless of course it is expired or current. Thanks for the responses so far.

Just for anyone curious, I have figured out a solution. Was easier than I thought; was just a matter of not being so lazy. Here is the solution:

<?php

    //Dates
    $current = date('m-d-Y');
    $expires = '2007-11-09'; //Date that the " " expires. Can be an expiration of anything. Can also be pulled from a database. Must be in Y M D formatt, unless you want to edit the code
    list($year, $month, $day) = explode('-', $expires);    
    $notice =  date('m-d-Y', mktime(0, 0, 0, $month, $day, $year) - (5 * 24 * 60 * 60)); //Subtract x amount of days (5 in this case) from the expiring date to start the expiring notice.
    
    //Date Status
    if ($current >= $expires)
    {
        echo 'Expired';
    }
    else if ($current >= $notice && $current <= $expires)
    {
        echo 'Expiring within 5 days';
    }
    else
    {
        echo 'Current';
    }

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.