URL中的日期dd/mm/yyyy [英] Date in a URL dd/mm/yyyy

查看:247
本文介绍了URL中的日期dd/mm/yyyy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下格式的URL传递日期(dd/mm/yyyy):

I am passing a date (dd/mm/yyyy) in URL with the following format:

http://www.website.com/_parameter=20/02/2000

我正在使用以下PHP将其转换为YYYY-MM-DD格式.

I am using the following PHP to convert it to the YYYY-MM-DD format.

<?php

    $D->query = '';

        if ($this->param('_parameter')) $D->query = date('Y-m-d', strtotime($this->param('_parameter'))); 

?>

我的数据库如下:

SELECT idtask FROM task WHERE unfinish=1 AND date LIKE '%".$D->query."%' "

以上返回以下内容:

1970-01-01

1970-01-01

推荐答案

使用 strotime() ,您需要确保使用的是有效的日期时间格式.否则,strtotime()将返回false 给您意外的值.

When using strotime() you need to make sure you are using a valid datetime format. Otherwise strtotime() will return false or give you an unexpected value.

使用XX/XX/XXXX格式的日期假定为美国格式,这意味着前两位数字代表月份,后两位数字代表月份,后四位数代表年份.使用破折号时,日期假定为欧洲格式.例如:

Dates that use XX/XX/XXXX format are assumed to be in US format which means the first two digits represent the month, the next two digits represent the day of month, and the last four digits represent the year. When dashes are used, the dates are assumed to be in European format. For example:

04/18/2017 = April 18, 2017
12/18/2017 = December 18, 2017
18-04-2017 = April 18, 2017
18-12-2017 = December 18, 2017

如果您不小心切换了日期和月份,则strtotime()将返回false,因为日期无效.

If you accidentally switch the day and month strtotime() will return false as the date is invalid.

18/04/2017 // error
18/12/2017 // error
04-18-2018 // error
12-18-2017 // error

上面的例子很简单.但是当日期不明确时,您可能会遇到问题.例如:

The above examples are straight forward. But you can run into issues when the dates can be ambigous. For example:

04/12/2017 = April 12, 2017
12/04/2017 = December 4, 2017
04-12-2017 = December 4, 2017
12-04-2017 = April 12, 2017

在上面的示例中,通过切换日期和月份,我们仍然可以获得有效的日期,这可能在您的应用程序中导致意外的结果.要解决这些潜在问题,建议使用 DateTime::createFromFormat() 来解析日期广告返回 DateTime() 对象,您可以从中获取Unix时间戳

In the above examples by switching the day and month we still get valid dates which can cause unexpected results in your application. To solve these potential issues it is recommended to use DateTime::createFromFormat() to parse the date ad return a DateTime() object from which you can get a Unix Timestamp, convert the date into another format, or use it to compare to other DateTime objects.

// Parse US date format
$date1 = DateTime::createFromFormat('m/d/Y', '04/18/2017');

// Get Unix timestamp of 1493581268
$timestamp = $date1->getTimestamp();

// Parse European date format
$date2 = DateTime::createFromFormat('d-m-Y', ''18-04-2017);

// Get MySQL format (ISO-8601) of 2017-04-18
$mysqlDate = $date2->format('Y-m-d');

另请参见:

对于您的具体情况,以下代码将起作用:

For your specific case, the follow code will work:

$date = $date1 = DateTime::createFromFormat('m/d/Y', '20/02/2000');
$D->query = $date->format('Y-m-d'); // 2000-02-20

这篇关于URL中的日期dd/mm/yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆