We help IT Professionals succeed at work.

PHP strtotime limit workaround??

birwin
birwin asked
on
Medium Priority
1,099 Views
Last Modified: 2012-05-06
Hi I am using this code to determine a date range. If a user selects the age 25-30 I want to compare that to a date in my mysql database.
My code is
 $today=date("Y-m-d");
//$age-group is  packed 25|30
 $age_gr=explode("|",$age_group);
 $start_date=date("Y-m-d", strtotime("$today - $age_gr[0] years"));
 $end_date=date("Y-m-d", strtotime("$today - $age_gr[1] years"));

This works perfectly, as long as the user is under 40.
Is there a work around to allow me to use any age up to 115?

 
$today=date("Y-m-d");
 
 $age_gr=explode("|",$age_group);
 $start_date=date("Y-m-d", strtotime("$today - $age_gr[0] years"));
 $end_date=date("Y-m-d", strtotime("$today - $age_gr[1] years"));

Open in new window

Comment
Watch Question

Most Valuable Expert 2011
Author of the Year 2014

Commented:
I'm sure we can work this out.  What are the age groups you want?
Most Valuable Expert 2011
Author of the Year 2014
Commented:
Also, what level of PHP and what platform are you using?

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Most Valuable Expert 2011
Author of the Year 2014

Commented:
This seems to get us back to about 1902.  Not trying to be a prick, but anybody born in 1902 is not likely to be using the WWW.

Does that help? ~Ray
<?php // RAY_age_groups.php
 
$age_groups = array(25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 106, 107, 108, 109, 110, 111, 115);
 
foreach ($age_groups as $years_ago)
{
   $birthday = date('Y-m-d', strtotime('today ' . "- $years_ago years"));
   echo "<br/>$years_ago YEARS AGO - BIRTHDAY IS $birthday \n";
}

Open in new window

Author

Commented:
Ray, I was wrong. My code did work. I had picked 35 to 40, saw the dreaded 1969 UNIX date, and assumed it didn't work, but 1969 was the right date for the range I chose.

You have helped me before, so I am glad to award the points.

Brian
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Amazing!  I would have looked at 1969 and thought the same thing.  ;-)  Thanks for the points and for the good question.  Best, ~Ray
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.