简体   繁体   English

URL中的日期dd / mm / yyyy

[英]Date in a URL dd/mm/yyyy

I am passing a date (dd/mm/yyyy) in URL with the following format: 我正在URL中以以下格式传递日期(dd / mm / yyyy):

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

I am using the following PHP to convert it to the YYYY-MM-DD format. 我正在使用以下PHP将其转换为YYYY-MM-DD格式。

<?php

    $D->query = '';

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

?>

And my database as following: 而我的数据库如下:

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

The above return the following: 上面返回以下内容:

1970-01-01 1970-01-01

When using strotime() you need to make sure you are using a valid datetime format . 使用strotime() ,需要确保使用有效的datetime格式 Otherwise strtotime() will return false or give you an unexpected value. 否则, strtotime()将返回false 为您提供意外的值。

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. 假定使用XX / XX / XXXX格式的日期为美国格式,这意味着前两位数字代表月份,后两位数字代表月份,后四位代表年份。 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

If you accidentally switch the day and month strtotime() will return false as the date is invalid. 如果您不小心切换了日期和月份,则strtotime()将返回false,因为日期无效。

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

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. 为了解决这些潜在的问题,建议使用DateTime::createFromFormat()解析日期广告并返回DateTime()对象,从该对象可以获取Unix时间戳将日期转换为另一种格式或将其与其他格式进行比较DateTime对象。

// 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');

See also: 也可以看看:


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM