2

I have a date stored in my DB (due_date) I am wanting to write a script that checks if the due_date is in the next 3 days

From checking online (google) i have found some code but it doesnt seem to work, see below

if (time() - filemtime($due_date) >= 3 * 86400) 

{
  echo" Invoice $id is due in the next 3 days<br />";
}
else
{
echo" Invoice $id not due in the next 3 days </br>";
}

$due_date contains a date in the format 01/01/2015

Please can someone help with this? P.s I am a newbie!

Thanks

4 Answers4

2

Use strtotime() to convert the date string to a unix timestamp, and edit your if statement accordingly:

$seconds_to_expire = strtotime($due_date) - time();
if ($seconds_to_expire < 3*86400) {
     ...

Note that dates in the m/d/y or d/m/y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed (see this). You may want to convert your date to a Y-m-d format instead:

$due_date_ymd = date('Y-m-d', strtotime(str_replace('/', '-', $due_date));
$seconds_to_expire = strtotime($due_date_ymd) - time();
if ($seconds_to_expire < 3*86400) {
    ...
Community
  • 1
  • 1
Aviram
  • 3,007
  • 4
  • 28
  • 43
1

Change

filemtime($due_date)

to

strtotime(str_replace('/', '-', $due_date))

you have to change / to - if the day comes first, otherwise php will assume that first is month!

n-dru
  • 9,174
  • 2
  • 27
  • 41
0

You could translate the DB date value (which is in the format 'yyyy-mm-dd') to a DateTime object and then compare:

$d = DateTime::createFromFormat('Y-m-d', $due_date);
$d->modify('-3 days');  // subtract 3 days from due date
if ($d < new DateTime()) {
    echo 'date is due within 3 days';
}

Notes:

  1. new DateTime() will give you the current date.
  2. I assume that $due_date is a string in the format 'yyyy-mm-dd', as you should get if it is a Date column in the database.

Since you commented that $due_date is "A date in the format 01/01/2015", you can easily ajust the code: change the format specifier in the createFromFormat function from 'Y-m-d' into 'd/m/Y'.

Marten Koetsier
  • 3,149
  • 2
  • 23
  • 36
0

if (strtotime($due_date)+60*60*24*3 =< time()+60*60*24*3) { echo "Is due in the next 3 days" } else { echo "Is not due in the next 3 days" }

Micael Dias
  • 331
  • 2
  • 12