Karen Dixon
Karen Dixon

Reputation: 43

PHP date: non well formed numeric value encountered

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?

Upvotes: 2

Views: 1359

Answers (2)

Mike Brant
Mike Brant

Reputation: 71422

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

Upvotes: 0

Summoner
Summoner

Reputation: 1407

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"]);

Upvotes: 0

Related Questions