PHP:帮助处理此日期格式

发布于 2024-10-16 00:52:03 字数 653 浏览 7 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

药祭#氼 2024-10-23 00:52:03

答案是欧洲日期格式使用破折号,而不是斜杠。

根据手册

注意:

日期采用 m/d/y 或 dmy 格式
通过查看来消除歧义
各个之间的分隔符
组件:如果分隔符是
斜杠 (/),则美式 m/d/y 为
假定;而如果分隔符是
破折号 (-) 或点 (.),然后
假定为欧洲 dmy 格式。

使用正确的格式就像一个魅力:

// 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

The answer is that the European date format uses dashes, not slashes.

According to the manual:

Note:

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.

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
Oo萌小芽oO 2024-10-23 00:52:03

我要做的就是用“/”分解日期并使用 mktime 创建一个新日期:

$from = explode('/', $_POST['from_date']);
$from = mktime(0, 0, 0, $from[1], $from[0], $from[2]);
$from = mdate($datestring, $from);

What I would do is explode the date by '/' and make a new date with mktime:

$from = explode('/', $_POST['from_date']);
$from = mktime(0, 0, 0, $from[1], $from[0], $from[2]);
$from = mdate($datestring, $from);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文