Solved

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

Posted on 2011-10-30
192 Views
\$date is in format 2011-08-30
if \$date is within 30 days of today
do something
0
Question by:rgb192

LVL 7

Expert Comment

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

\$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

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

LVL 11

Expert Comment

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 107

Accepted Solution

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

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

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 107

Expert Comment

@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

Best checked answer. Thanks
0

## Join & Write a Comment Already a member? Login.

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

#### 732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!