PHP:有关此日期格式的帮助 [英] PHP: Help with this date formatting
问题描述
我有一个应用程序,我正在使用CodeIgniter。我在SQL Server DB中有记录,它们有datetime字段。
I have an application I'm building with CodeIgniter. I have records in a SQL Server DB which have datetime fields.
我从m / d / Y中的文本字段查询这些记录。这对应于数据库中的日期格式。
I'm querying these records from dates entered into a textfield in the m/d/Y. This corresponds to the date format in the db.
不幸的是我在英国!所以我想输入日期像d / m / Y。所以我需要捕获这个,格式化成适当的格式,然后验证它。
Unfortunately I'm in the UK! So I want to enter dates in like d/m/Y. So I need to capture this, format it into the appropriate format and then validate it.
以下是使用格式化的代码:
Here's the code in using to format:
$datestring = "%m/%d/%Y";
if(isset ($_POST['from_date']) AND isset ($_POST['to_date'])){
$from = mdate($datestring, strtotime($_POST['from_date']));
$to = mdate($datestring, strtotime($_POST['to_date']));
我使用CI日期助手的mdate,但它几乎与date认为。
I'm using mdate from the CI date helper but it's pretty much the same as date() I think.
如果我输入数据13/12/2010,然后发布表单,并转储其出现的新日期
If I enter the data 13/12/2010 then post the form and dump the new date it comes out as
string(10) "02/06/2011"
$ b b
这是怎么回事?
How'd this happen?
有人可以借钱吗? :D
Can someone lend a hand? :D
比利
推荐答案
答案是欧洲日期格式使用破折号,而不是斜杠。
The answer is that the European date format uses dashes, not slashes.
根据手动:
注意:
Note:
通过查看各个
组件之间的
分隔符,可以消除m / d / y或dmy格式
中的日期:如果分隔符是
斜杠(/),那么美国m / d / y是假定的
;如果分隔符是
dash( - )或点(。),则假定
欧洲dmy格式。
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
使用正确的格式像一个charm:
Using a correct format works like a charm:
// American format
//
$_POST['from_date'] = "02/06/2011";
$yankeeTimestamp = strtotime($_POST['from_date']);
// European format
//
$_POST['from_date'] = "06-02-2011";
$euroTimestamp = strtotime($_POST['from_date']);
echo date("m/d/Y", $yankeeTimestamp); // returns 02/06/2011
echo date("m/d/Y", $euroTimestamp); // returns 02/06/2011
这篇关于PHP:有关此日期格式的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!