-1

I am trying to find the date not less than 18 years, I tried this following code, but its not working for me.

// validate birthday
function validateAge($then, $min)
{
    // $then will first be a string-date
    $then = strtotime($then);

    echo "<br>";
    echo 'test1-';
    var_dump( $then );

    exit;
    //The age to be over, over +18
    $min = strtotime('+18 years', $then);

    if(time() < $min)  {
        die('Not 18'); 
    }
}

$res = validateAge('2016-02-29', $min = 18);

var_dump($res);

I fyou see the above question, you can see that, date is not valid, even if i pass the wrong date, its shows the $then = strtotime($then);

var_dump($then) show the int

my question is, how its printing the timestamp, event if we passing the invalid date.

user3929758
  • 233
  • 2
  • 3
  • 15
  • If you look at the lines you've written, you'll find out why it doesn't work, easily. – revo May 13 '16 at 08:21
  • 1
    Possible duplicate of [Calculating number of years between 2 dates in PHP](http://stackoverflow.com/questions/5387166/calculating-number-of-years-between-2-dates-in-php) – Marius May 13 '16 at 08:25

5 Answers5

1

Your logic is correct. Remove die, exit and echo which is not needed

function validateAge($then, $min)
{
    // $then will first be a string-date
    $then = strtotime($then);

    //The age to be more then min years
    $min = strtotime('+'. $min . ' years', $then);

    return time() > $min;
}

$res = validateAge('2016-02-29', $min = 18);
echo $res ? 'O\'key' : "Not $min years"; 

demo

Saurabh
  • 776
  • 1
  • 5
  • 15
splash58
  • 26,043
  • 3
  • 22
  • 34
  • I probably searched too complicated. :s – Alexis May 13 '16 at 08:24
  • I downvoted because this is not a question to be answered. Instead, it's better to vote for close. – revo May 13 '16 at 08:42
  • @revo it's your right, but i think that in this case more orrect to downvote question and vote to close it. While rewardeding an answer is independent process – splash58 May 13 '16 at 08:56
  • @splash58 It's community right and yes, I downvoted both. – revo May 13 '16 at 08:59
  • even if i pass the wrong date, its shows the $then = strtotime($then); var_dump($then) show the int my question is, how its printing the timestamp, event if we passing the invalid date. – user3929758 May 13 '16 at 09:33
0

Try maybe something like this

function compareAge($date,$min=18)
{
    $strdate = strtotime($date);
    $curdate = strtotime("today");
    $datefin=date("Ymd",$curdate)-date("Ymd",$strdate);
    $age=substr($datefin,0,strlen($datefin)-4);
    return $age>=$min;
}

var_dump(compareAge("2013-05-13"));

DEMO

Alexis
  • 5,681
  • 1
  • 27
  • 44
0

you could use this method:

public function validateAge($then)
{
  $then= date_create($then);
  $now = date_create("now");

  $diff = $now->diff($then);

  if ($diff->y > 18)
  {
  die('not 18');
  }

}

Duplicate: Calculating number of years between 2 dates in PHP

Community
  • 1
  • 1
Marius
  • 399
  • 2
  • 14
0

use the datetime object to save all sorts of pain. Its so much more simple.

function validateAge(DateTime $then, $min = 18)
{
    $now = new DateTime();

    $minimum = clone($now);         // you could just modify now, but this is simpler to explain
    $minimum->modify("-$min years");


    if($then < $minimum) {
        return false;
    } 

    return true;
}

echo validateAge(new DateTime('1-1-1997')) ? 'true' : 'false';  // returns false
echo validateAge(new DateTime('1-1-1999')) ? 'true' : 'false';  // returns true

see example

DevDonkey
  • 4,835
  • 2
  • 27
  • 41
0

Wow, so many try-hards. In case you like is simple:

<?php
function validateAge($date) {
    return date_create('18 years ago') > date_create($date);
}

var_dump(
    validateAge('2010-10-05'),
    validateAge('1992-09-02')
);

OUTPUT

bool(false)
bool(true)

Play with me on 3v4l.org

Edit: Also works with the $min parameter:

<?php
function validateAge($date, $min) {
    return date_create("$min years ago") > date_create($date);
}

var_dump(
    validateAge('2010-10-05', 18),
    validateAge('1992-09-02', 18)
);
ml_
  • 392
  • 2
  • 9