2

I'm trying to store a date from a PHP form into an MS SQL database and am really struggling. The date is in string format, and in 'DD/MM/YYYY' format so I've tried to use strtotime() to convert it, but I just get 1970-01-01 01:00:00 from that.

The code I've tried is the following:

$requested = $_REQUEST["TextboxRequiredBy"];
var_dump($requested);
echo "<br/>";
$daterequested = date("Y-m-d H:i:s", strtotime($requested));
echo $requested . " becomes " . $daterequested . "<br/>";
mssql_bind($stmt, "@Parameter", $daterequested, SQLFLT8, false);

What I get on screen is:

string(10) "31/07/2012" 
31/07/2012 becomes 1970-01-01 01:00:00

Warning: mssql_bind(): Unable to set parameter in xxx.php on line 110 
Warning: mssql_execute(): message: Procedure or function 'SPNAME' expects 
parameter '@Parameter', which was not supplied. (severity 16) in xxx.php

I've searched around and I just can't seem to find a way to convert the string successfully. I've used the following on another page that runs a date comparison, but this brings back an error that date_format() expects parameter 1 to be DateTime, boolean given:

$datefrom = date_create($requested);
$daterequestedfrom = date_format($datefrom,'Y-m-d H:i:s');

Can anyone suggest how I can successfully convert the string into a date?

Zhorov
  • 28,486
  • 6
  • 27
  • 52
Karen Dixon
  • 43
  • 1
  • 4

2 Answers2

0

For some reason, PHP doesn't like the slashes in strtotime. Convert them to dots and the script should work.

$requested = str_replace('/', '.', $_REQUEST["TextboxRequiredBy"]);
Summoner
  • 1,397
  • 1
  • 9
  • 9
0

That date format is not a default format. You probably want to look at the date_create_from_format function. This will allow you to specify the format of the time string you are trying to input.

Link to PHP documentation

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • And now I feel a complete idiot... I use the JQuery datePicker on the form fields on this page and another one that successfully compares the date entered with the date stored in the database. Your answer made me realise that I hadn't used the exact same function call, and that the date format on both was set differently! Changed that and now it works fine. Thanks for the info, will look at using that function in the future! – Karen Dixon Jul 31 '12 at 06:49