Solved

# if date is within 30 days of today, do something

Posted on 2011-10-30
Medium Priority
249 Views
\$date is in format 2011-08-30
if \$date is within 30 days of today
do something
0
Question by:rgb192
• 3
• 2
• 2
• +2

LVL 7

Expert Comment

ID: 37055184
I would take the given date, convert it to a UNIX timestamp (number of seconds past January 1st, 1970), calculate how many days it was, then compare against the number of days it currently has been since the UNIX epoch. This is done using the strtotime() function.

I take it you've not really programmed before, since this is pretty elementary stuff. Just to let you know, if you're doing this for homework, expecting us to do the work for you, this is considered cheating, and violates ToS. If caught, the post will be deleted, and you can be banned from Experts-Exchange. Being banned would suck especially for you since you're listed as a Premium Service member,thus meaning you paid to be here. Also, if this is for homework, you could have easily coded what you want to do in far less time than it takes to get a response.

If this is not the case, and this is not for homework, I do have a functioning code example that I can post. I would also recommend structuring your questions differently. Simply asking people to do something will not give you any more than a basic idea. You have to show that you're actively trying, and that you need assistance. Once people see that you've tried, but are having problems, you'll get plenty of people that will help you out. We're here to help those in need, not those wanting hand outs.
0

LVL 9

Expert Comment

ID: 37055211
\$d1->format("Y-m-d");
\$d2 = date(Y-m-d);

\$Hourdiff = floor((\$d2-\$d1)/3600) ;
\$Minutediff =floor((\$d2-\$d1)/60);
\$Monthdiff =floor((\$d2-\$d1)/2628000);
\$daydiff =floor((\$d2-\$d1)/86400) ;
\$yeardiff =floor((\$d2-\$d1)/31536000) ;

if (\$daydiff=30)
{
// write year code here
}

0

Author Comment

ID: 37055634
this is not for homework
and I do not understand keyu example
if \$date=2011-08-30
0

LVL 11

Expert Comment

ID: 37055789
Hello rgb192,

\$date1 = "2011-08-11";
\$date2 = date("Y-m-d");

\$days = 0;

\$days = round((strtotime(\$date2)-strtotime(\$date1))/86400);

if(\$days > 0 && \$days < 30)
{
//do something
}

Thank You.

Amar.
0

LVL 111

Accepted Solution

Ray Paseur earned 2000 total points
ID: 37056269
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Example of how to find the limits of dates "within" 30 days of today:
\$past = date('c', strtotime('Today - 30 days'));
\$future = date('c', strtotime('Today +30 days'));

Example of how to compare an ISO-8601 DATETIME string to these values:
\$date = "2011-08-30";
if ( (\$date >= \$past) && (\$date <= \$future) ) echo "\$date IS WITHIN 30 DAYS OF TODAY";
0

LVL 9

Expert Comment

ID: 37069124
Hi,

\$d1="2011-08-11";
\$d2 = date(Y-m-d);

\$Hourdiff = floor((\$d2-\$d1)/3600) ; // Give you difference between 2 days in hours
\$Minutediff =floor((\$d2-\$d1)/60); // Give you difference between 2 days in minutes
\$Monthdiff =floor((\$d2-\$d1)/2628000); // Give you difference between 2 days in Months
\$daydiff =floor((\$d2-\$d1)/86400) ; // Give you difference between 2 days in days
\$yeardiff =floor((\$d2-\$d1)/31536000) ; // Give you difference between 2 days in years

if (\$daydiff=30)  // here you are comparing day difference by comparing todays date with original one is equal to 30 days or not.
{
// write year code here
}

Hope this helps you now.

Thanks,
0

LVL 9

Expert Comment

ID: 37069129
Hi,

\$d1="2011-08-11";
\$d2 = date(Y-m-d);

\$Hourdiff = floor((\$d2-\$d1)/3600) ; // Give you difference between 2 dates in hours
\$Minutediff =floor((\$d2-\$d1)/60); // Give you difference between 2 dates in minutes
\$Monthdiff =floor((\$d2-\$d1)/2628000); // Give you difference between 2 dates in Months
\$daydiff =floor((\$d2-\$d1)/86400) ; // Give you difference between 2 dates in days
\$yeardiff =floor((\$d2-\$d1)/31536000) ; // Give you difference between 2 dates in years

if (\$daydiff=30)  // here you are comparing day difference by comparing todays date with original date ans verify is the date difference equal to 30 days or not.
{
// write year code here
}

Hope this helps you now.

Thanks,
0

LVL 111

Expert Comment

ID: 37069255
@rgb192:

Not all of the experts who post information here test their code before they post it, so I recommend that you, as the author of the question, do the tests for them.  I either provide a warning that a code block is untested, or I provide a demonstration script that shows you the exact extent of testing that has been applied to the code.  But I am a professional, and I hold myself to higher standards than most.

For example, consider this statement from the post by keyu at ID:37069124 and at ID:37069129:

\$d2 = date(Y-m-d);

I can tell you with metaphysical certainty that line of code has never been tested.  If it had been tested, it would never have been posted.  Caveat Emptor!
0

Author Closing Comment

ID: 37079475
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page