PHP to check date is not past 3 months from the current date

I am developing a PHP script and need to check whether the date the user has entered is not beyond 3 months from the current dates date. How would I go about this?

Thanks

For a Unix timestamp you can do this:

if ($time < time() - strtotime('-3 months')) {
	// older than three months
}

Same concept applies to other formats, but you can easily convert them to Unix timestamps using the strtotime() function.

The best bet for this is to use the unix timestamp.

For the first step, you have to get back the date the user gave you. What you need is to separate the day, month and year.
Then, you can create the timestamp of this date, with mktime() and check that time()-{the number of seconds in 3 months} is still bigger than the given date.


$userTstamp=mktime(0,0,0,$month, $day, $year); //given that the 3 variables are holding the user date elements.
$now=time()
$delta=3600*24*30*3;  //3600 seconds per hours *24 hours * 30 day * 3 months
if($userTstamp>($now-$delta)){
  echo "date ok";
}
else{
  echo "date too old";
}

Fixed

Uhm… no. Not fixed - broken.

The current time plus three months will always be higher than a timestamp from the past.

The idea is that you subtract three months from the current time so you’ll have the timestamp from three months earlier. You’ll then compare it with the other timestamp you have. If the other timestamp is lower then it must be from an earlier point, i.e. it’s older than three months.

There is quite a bit wrong with this post.

First of all, you didn’t read the OP very well, and are not understanding his request. Secondly, you implemented the misunderstanding incorrectly.

Let me cover why my solution is correct, and then I will cover why your solution to the misunderstood problem is incorrect.

I am developing a PHP script and need to check whether the date the user has entered is not beyond 3 months from the current dates date. How would I go about this?

Let me rephrase his request.

I need to check a date a user enters, and make sure that date is not beyond 3 months from now.

You misinterpreted this as:

I need to check a date a user enters, and see if that is older than 3 months.

Therefore, my solution written in PHP and English:

if ($time < strtotime('+3 months')) {
    // Time is not beyond 3 months
}
If [time the user enters] is less than a date [three months from now] then
   Time is not beyond 3 months

Exactly like he asked for.

Now, even though you misinterpreted what he is asking for, you implemented your solution wrong too!

A correct solution to:

I need to check a date a user enters, and see if that is older than 3 months.

Would be:

if ($time < strtotime('-3 months')) {
        // Date is older than three months
}
If [time the user enters] is less than [three months ago] then
    Time is older than three months

Your solution makes no sense.

How can I convert the date string (in dd/mm/yyyy format) to time to use with the above code?

Thanks

Any help?

Thanks

list($day, $month, $year) = explode('/', '29/12/2008');
$timestamp = mktime(0, 0, 0, $month, $day, $year);

or

list($day, $month, $year) = explode('/', '29/12/2008');
$timestamp = strtotime("$year-$month-$day");