-1

I'm having some strange issues with a simple $dob form input and database insert with PDO into a mysql table of datatype date.

Input Form (example):

<select name="dob-month">
    <option value="1">January</option>
</select>
<select name="dob-day">
    <option value="01">1</option>
</select>
<select name="dob-year">
    <option>2013</option>
</select>

PHP (basic):

$data = $_POST; //passed through mvc

$day = trim($data['dob-day']);
$month = trim($data['dob-month']);
$year = trim($data['dob-year']);

$birthdate = date('Y-m-d', strtotime($year."-".$month."-".$day));

$stmt = $this->core->dbh->prepare("INSERT INTO userinfo (birthdate) VALUES (:birthdate)");
$stmt->bindParam(':birthdate', $birthdate, PDO::PARAM_STR);
$stmt->execute();

But it's giving dates without the day? so just 2013-12 work correctly but the day is always -01?

Is it to do with the select format of the date?

Dan
  • 11,914
  • 14
  • 49
  • 112
  • what field type is birthdate? –  Aug 27 '13 at 22:49
  • what does $year, $month and $day are, And maybe you need strtotime() ? – Royal Bg Aug 27 '13 at 22:51
  • Try with `strtotime("$day $month $year")` and also take care what locale does with this. Your question reminds me a bit of: [strtotime With Different Languages?](http://stackoverflow.com/q/6988536/367456) and [how to php convert this time format?](http://stackoverflow.com/q/11931209/367456). – hakre Aug 27 '13 at 23:06

3 Answers3

1

Second parameter of

  date()

is an integer value (a natural number), not a dash separated string. Read the man-page of date().

Quite like you'd like to do something like this:

date('Y-m-d', strtotime( $year."-".$month."-".$day ));

At the other side: This operation constructs a date string passes it along to date() - which in turn returns the same date string ;-)

SteAp
  • 11,853
  • 10
  • 53
  • 88
  • When inserting a date into a `date` should you use the timestamp or string of the date in format `'Y-m-d'`? – Dan Aug 27 '13 at 23:02
  • @Silver89 No you pass an integer value as the second parameter to date(). The integer value is a UNIX timestamp. The source of the current time as a UNIX timestamp is time(). Therefore, date('Y-m-d', time()) is just fine. – SteAp Aug 27 '13 at 23:07
1

First of all, the PHP date() command accepts only an official UNIX timestamp to be converted into the specified format. Secondly, while your form has variables named dob-month, dob-day and dob-year, it looks as if you are trying to use them in the variables $month, $day and $year in your PHP processing script. Why don't you start with a nice PHP date tutorial explaining the difference between a DATE STRING, a DATE OBJECT, and a UNIX TIMESTAMP. Read up on PHP's strtotime() function, and mySQL's DATE_FORMAT() function. By the time you get done with those things, you should be informed enough to return and submit a much moire educated question.

edits / additional info

<select name="dob-month">
    <option value="1">January</option>
    <option value="6">June</option>
    <option value="12">December</option>
</select>
<select name="dob-day">
    <option>1</option> // Same here - stick to a single digit
    <option>15</option>
    <option>31</option>
</select>
<select name="dob-year">
    <option>2013</option>
</select>

PHP

$day = intval(trim($data['dob-day']));
$month = intval(trim($data['dob-month']));
$year = intval(trim($data['dob-year']));
$birthdate = date('Y-m-d', mktime(0,0,0,$month,$day,$year));
DevlshOne
  • 8,357
  • 1
  • 29
  • 37
  • Thanks for the sarcasm, I edited the php due to the post being passed through a controller and didn't want to confuse everyone then forgot to put it back in. I understand the types and even with `$birthdate = date('Y-m-d', strtotime($year."-".$month."-".$day));` I'm still getting issues? – Dan Aug 27 '13 at 22:56
  • It was hardly sarcasm. It remains a poorly worded question with just BARELY enough supporting code to figure out what;s going on with your script(s). `strtotime()` may not be reliable enough to use in this situation. You may have to go the long way with `mktime()`. – DevlshOne Aug 27 '13 at 22:58
  • echo `$birthdate` a few times and see if it's not getting obliterated due to the `strtotime()` conversion. – DevlshOne Aug 27 '13 at 23:02
0

Use this code

$birthdate = $_POST['dob-year']."/".$_POST['dob-month']."/".$_POST['dob-date'];

Instead of this code

$birthdate = date('Y-m-d', strtotime($year."-".$month."-".$day));
$birthdate = date('Y-m-d', mktime(0,0,0,$month,$day,$year));