PHP:帮助处理此日期格式
我正在使用 CodeIgniter 构建一个应用程序。我的 SQL Server 数据库中有包含日期时间字段的记录。
我正在从 m/d/Y 文本字段中输入的日期查询这些记录。这对应于数据库中的日期格式。
不幸的是我在英国!所以我想输入日期,例如 d/m/Y。所以我需要捕获它,将其格式化为适当的格式,然后验证它。
下面是使用格式化的代码:
$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() 几乎相同。
如果我输入数据 13/12/2010 然后发布表格并转储新日期,它会显示为“
string(10) "02/06/2011"
这是怎么发生的?”
有人可以帮忙吗? :D
比利
I have an application I'm building with CodeIgniter. I have records in a SQL Server DB which have datetime fields.
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.
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']));
I'm using mdate from the CI date helper but it's pretty much the same as date() I think.
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"
How'd this happen?
Can someone lend a hand? :D
Billy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案是欧洲日期格式使用破折号,而不是斜杠。
根据手册:
使用正确的格式就像一个魅力:
The answer is that the European date format uses dashes, not slashes.
According to the manual:
Using a correct format works like a charm:
我要做的就是用“/”分解日期并使用 mktime 创建一个新日期:
What I would do is explode the date by '/' and make a new date with mktime: