0

Would the date function for php recognize this code, and convert it to a Date Stamp

$date= date('$_POST["Month1"]/$_POST["Date1"]/$_POST["Year1"]');

?

Anthony
  • 36,459
  • 25
  • 97
  • 163
  • 2
    No, because of [single quotes](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php). – BlitZ Feb 14 '14 at 09:33
  • please show var_dump($_POST); – Dezigo Feb 14 '14 at 09:35
  • PHP handles variables inside [single quotes](http://us1.php.net/manual/en/language.types.string.php#language.types.string.syntax.single) as plain strings, not as variables. – Anthony Feb 14 '14 at 09:37
  • and the format? take a look at the documentation http://es1.php.net/manual/es/function.date.php – aleation Feb 14 '14 at 09:37
  • @aleation - oh, man, I didn't even notice that it was the wrong function. driving with blinders, gets me every time. – Anthony Feb 14 '14 at 09:45

2 Answers2

1

Your syntax is unclear and not works because of single quotes. Better try with:

$date = date($_POST["Month1"] . '/' . $_POST["Date1"] . '/' . $_POST["Year1"]);

If your post data contains format, like m, d, Y - it's ok. But if you pass date like 4, 21, 2014 - date() will not work. Convert it to timestamp with:

$timestamp = strtotime($_POST["Month1"] . '/' . $_POST["Date1"] . '/' . $_POST["Year1"]);
hsz
  • 148,279
  • 62
  • 259
  • 315
0

Your syntax is wrong, as already stated, but it's also better for avoiding formatting issues to actually just pass in the variables individually to mktime(), like this:

$timestamp = mktime(0, 0, 0, $_POST["Month1"], $_POST["Date1"], $_POST["Year1"]);

That way you aren't having to pass the variables you already have broken up through any extra steps.

Anthony
  • 36,459
  • 25
  • 97
  • 163