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

查看:29
本文介绍了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

推荐答案

当使用 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 时间戳, 将日期转换成另一种格式,或者用它来与其他格式进行比较DateTime 对象.

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天全站免登陆