drrhr20884 2017-05-02 19:46
浏览 108
已采纳

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

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

?>

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

  • 写回答

1条回答 默认 最新

  • dqce48404 2017-05-02 19:47
    关注

    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.

    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
    

    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
    

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

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题