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,017
  • 4
  • 29
  • 43
  • Thanks, however when I run this code, All invoices show "not due" but in my db i have dates of 09/03/2015 and 09/03/2015 (today and tomorrow?) – user4547025 Mar 08 '15 at 16:02
  • Oh, so in that just flip the statement, see my edited version above – Aviram Mar 08 '15 at 16:04
  • @user4547025 check what manual says about strtotime and slashes and look at my answer – n-dru Mar 08 '15 at 16:15
  • @Aviram How would i amend this for 1 day? So anything due tomorrow? – user4547025 Mar 08 '15 at 16:27
  • Just change the ```3*86400``` to something else. If you want a day you could write ```1 * 24 * 60 * 60```, if you want two days you could write ```2 * 24 * 60 * 60```. – Aviram Mar 08 '15 at 16:28
  • @Aviram - Would this work if (time() - strtotime($due_date) <= 86400) – user4547025 Mar 08 '15 at 16:33
  • It should work because 86400 equals to 1 * 24 * 60 * 60 (24 hours, 60 minutes per hour, 60 seconds per minute), but take a look at the updated code above, I made it safer. – Aviram Mar 08 '15 at 16:39
  • I now have the code - if (time() - strtotime($due_date) <= 86400) but it seems to pick everything up, not dates that are tomorrow? – user4547025 Mar 08 '15 at 16:45
  • You need to subtract time() from the due date (like my answer above). – Aviram Mar 08 '15 at 16:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72515/discussion-between-user4547025-and-aviram). – user4547025 Mar 08 '15 at 16:48
  • I have used your code above but nothing happens when i run the script. My dates are in the format d/m/Y – user4547025 Mar 08 '15 at 16:51
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,285
  • 2
  • 29
  • 42
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,389
  • 2
  • 25
  • 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