-1

I'm trying to get the date from the date-time-picker name="dob" into the php but it only post 2017 in the database user_age column. However, if I assigned $dob manually to $dob = '10/09/1988' it works.

<div class="form-group date-time-picker label-floating is-empty">
 <label class="control-label">Birthday</label>
 <input name="datetimepicker">
</div>

And here is the php

<?php
 $dob = $_GET['datetimepicker'];
 $dob = explode("/", $dob);
 $agv = (date("md", date("U", mktime(0, 0, 0, $dob[0], $dob[1], $dob[2]))) > 
 date("md") ? ((date("Y") - $dob[2]) -1) : (date("Y") - $dob[2]));

 $age = mysqli_real_escape_string($conn, $agv);
?>
ViajanDee
  • 654
  • 4
  • 15
  • 1
    what format is the date being submitted in from the datepicker? dd/mm/yyyy? mm/dd/yyyy? Something else? How are you verifying in the server that the date format was valid and expected? – ADyson Nov 14 '17 at 13:48
  • according to the js of the datetimepicker, the format is MM/DD/YYYY – ViajanDee Nov 14 '17 at 13:50
  • 2
    What are the exact contents of `$_GET['dob']`? – jeroen Nov 14 '17 at 13:52
  • 1
    Possible duplicate of [Calculating age from date of birth in PHP](https://stackoverflow.com/questions/10186811/calculating-age-from-date-of-birth-in-php) – splash58 Nov 14 '17 at 13:53
  • @jeroen It should pull the value of a datetimepicker inside the div above with format MM/DD/YYYY. – ViajanDee Nov 14 '17 at 13:55
  • 2
    I don't really care what it should do, I want to know what it contains exactly. – jeroen Nov 14 '17 at 13:56
  • 1
    @FMeshreky, should do, or actually does do? Big difference. You can test that pretty straightforwardly – ADyson Nov 14 '17 at 13:56
  • jcorry's answer is correct assuming you have a valid date in mm/dd/yyyy format (or any other other formats deemed acceptable in the docs: http://php.net/manual/en/datetime.formats.date.php) – ADyson Nov 14 '17 at 13:58
  • 2
    you have two `name` attributes for you input. The `datetimepicker` one probably should go somewhere else, maybe `type` (as `type="date"`) or `class` – Karsten Koop Nov 14 '17 at 13:58
  • @jeroen what I mean is it refers to the with name="dob" value and pulls the data inserted by the user. Sorry if I didn't understand the purpose of your comment. – ViajanDee Nov 14 '17 at 14:09
  • You have 1 input field. It has 2 'name' attributes. That's not legal HTML. No wonder PHP has no idea what is in $_GET['dob']. – John Corry Nov 14 '17 at 14:11
  • @KarstenKoop Thanks for the note I changed it to id – ViajanDee Nov 14 '17 at 14:12
  • @jcorry I changed it and now it is referring to name="datetimepicker' but I've got the same post to the database as 2017 with my code and 00 with your code below – ViajanDee Nov 14 '17 at 14:16
  • Because now the value is in $_GET[‘datetimepicker’] – John Corry Nov 14 '17 at 14:18
  • @jcorry I've edited that. I have a second doubt at the output of datetimepicker though. would you help me to post as a text it somehow to double check? – ViajanDee Nov 14 '17 at 14:21
  • Are you submitting the form with method="GET"? The form data will be in the $_POST array if the method = "POST". – John Corry Nov 14 '17 at 14:23
  • @jcorry yes the main form is using GET method – ViajanDee Nov 14 '17 at 14:28
  • 1
    @jcorry now it works when I changed both methods to POST. Thank you, very much appreciated. – ViajanDee Nov 14 '17 at 14:31

1 Answers1

2

I think the problem is to get a DOB, subtract that from the date of now and present the difference in years, right?

That seems like a problem for DateTime::diff

$dob = new DateTime($_GET['dob']); // we'll just assume this is a safe date value for now
$now = new DateTime();

$age = $dob->diff($now);

$ageInYears = $age->format('%Y');
John Corry
  • 1,567
  • 12
  • 15
  • 1
    Thanks, it returns 00 now no matter what the date I pick is. – ViajanDee Nov 14 '17 at 14:07
  • @FMeshreky I see you updated your question. Try this answer but with `$_GET['datetimepicker']` instead. If you change the field name on the client (i.e. the HTML form), you must change it on the server too. – ADyson Nov 14 '17 at 14:20
  • @ADyson yes I did changed it in the php as well and made sure that all variables connected but something is wrong with the math or the datetimepicker output – ViajanDee Nov 14 '17 at 14:26