PHP date:遇到的非格式的数值。

时间:2022-06-24 09:58:13

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.

我试图将一个PHP表单中的日期存储到一个MS SQL数据库中,但我真的很纠结。日期是字符串格式的,在'DD/MM/YYYY'格式中,所以我试着用strtotime()来转换它,但是我只得到了1970-01-01 01:00:00。

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:

我到处找过了,我似乎找不到一种方法来成功地转换字符串。我在另一个运行日期比较的页面中使用了下面的内容,但是这又带来了一个错误,date_format()期望参数1为DateTime, boolean为:

$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?

有人能建议我如何成功地把字符串转换成日期吗?

2 个解决方案

#1


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.

日期格式不是默认格式。您可能需要查看date_create_from_format函数。这将允许您指定要输入的时间字符串的格式。

Link to PHP documentation

PHP文档链接

#2


0  

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

出于某种原因,PHP不喜欢strtotime的斜杠。将它们转换成点,脚本就可以工作了。

$requested = str_replace('/', '.', $_REQUEST["TextboxRequiredBy"]);

#1


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.

日期格式不是默认格式。您可能需要查看date_create_from_format函数。这将允许您指定要输入的时间字符串的格式。

Link to PHP documentation

PHP文档链接

#2


0  

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

出于某种原因,PHP不喜欢strtotime的斜杠。将它们转换成点,脚本就可以工作了。

$requested = str_replace('/', '.', $_REQUEST["TextboxRequiredBy"]);