-1

I'm trying to get posted data of someones birthday, to send over to another page and display their age, so far I've got this which works

<?php
$c= date('Y');
$y= date('Y', strtotime('1993-12-29'));
echo $c - $y;
?>

But how would I do this with posted data? I've tried:

$c= date('Y');
$y= date('Y',strtotime($_POST['sBirthday']));
echo $c-$y;

Although this doesn't work, any ideas?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ili
  • 139
  • 1
  • 12
  • 2
    Check the *actual* value with `var_dump($_POST['sBirthday']);`. For php it does not really matter where it comes from. What matters is the value. – zerkms Feb 10 '15 at 23:28
  • Show your form... Also you can var_dump($_POST) to see what is sent... – Whirlwind Feb 10 '15 at 23:28
  • Problem's most likely in your form. How's it's used, the method, missing name attribute; could be anything. Input in it would need to follow what you have `1993-12-29` or `19951229` – Funk Forty Niner Feb 10 '15 at 23:34
  • possible duplicate of [PHP calculate age](http://stackoverflow.com/questions/3776682/php-calculate-age) – kero Feb 10 '15 at 23:37
  • `19951229` I meant `19931229` but it's not much different. Give/take a few years ;-) – Funk Forty Niner Feb 10 '15 at 23:48
  • Duplicate [http://stackoverflow.com/a/19521323/67332](http://stackoverflow.com/a/19521323/67332). – Glavić Feb 11 '15 at 05:57
  • @Glavić I do not think, this is his question, since he assumes that the first code works; even though it includes the same problem, which to solve you imply would be the question... but it clearly is not. – Matteo B. Feb 11 '15 at 09:23

1 Answers1

1
  1. Your posted code will work with the according HTML-Form if the person puts it in correctly. For example this form will work, if the user types 1993-12-29:
<form method="post" action="">
  <input type="date" name="sBirthday" />
  <input type="submit" />
</form>
  1. Your code does not actually calculate the age of a person. Think, somebody is born on 2014-12-31; would you say he is 1 year old (this is what your code would calculate)? In fact he is 0 years old, because he did not live for a full year yet.

    To calculate the age of a person try as suggested here:

$date = new DateTime($_POST['sBirthday']);
$now = new DateTime();
$interval = $now->diff($date);
echo $interval->y;
Community
  • 1
  • 1
Matteo B.
  • 3,906
  • 2
  • 29
  • 43
  • *"For example this form will work, if the user types `1993-12-29`"* - As will `19931229` - It will use the first four integers. – Funk Forty Niner Feb 10 '15 at 23:50
  • @Fred-ii- Actually, no. The [manual states](http://php.net/manual/en/datetime.construct.php) that the first argument to the constructor of DateTime is 'A date/time string. Valid formats are explained in [Date and Time Formats](http://php.net/manual/en/datetime.formats.date.php).'. The list of viable formats does not contain `YYMMDD` which would be what you suggested (`19931229`). Or did I understand your comment wrong? – Matteo B. Feb 11 '15 at 00:25
  • My bad, however I was referring to OP's code using `strtotime` and not about yours. – Funk Forty Niner Feb 11 '15 at 01:14