duan0531 2015-06-25 22:13
浏览 125
已采纳

PHP回显来自HTML表单的日期,并将其格式化为Y-m-d到d-m-Y错误31-12-1969

I have a text link to pass a date GET in format Y-m-d because is comming from MySQL like that. The page receiving this GET uses the follow code to echo the date I got from previous page

the link is like this

page.php?thedate=2015-06-30

The page I need to show this date must convert this date like this

<?php echo date('d-m-Y',strtotime($_GET["thedate"]));?>

so we show 30-06-2015

But it show with this code

31-12-1969

What can the problem be?

  • 写回答

1条回答 默认 最新

  • doujiyong7604 2015-06-25 22:39
    关注

    Your code is looking OK. But few thoughts, what could go wrong:

    At first: Try the following code:

    <?php
        var_dump($_GET['thedate']); // check what it is?
        date_default_timezone_set('Europe/Warsaw'); // set default timezone to your zone / user zone
        echo date('d-m-Y',strtotime('2015-06-30')); // check that date is working with plain text
    ?>
    

    At first remember to always use date_default_timezone_set('Europe/Warsaw');. It can be really important for Your application. Second, enter the date in plain text mode. If this works, it mean that the date function is working ok, and there is something wrong with Your GET or after GET variable manipulation.

    Second: NEVER, EVER DON'T VALIDATE _GET VARIABLES. Sorry for double negation, but maybe becouse of that, it can be more memorable ;). Always parse the data that user enters in the URL. Always parse:

    • _GET data
    • _POST data
    • _REQUEST data
    • _SERVER data (yes, some of them can be manipulated
    • all other vulnerable data...

    So summing up: Your code (the one You entered is OK), check the GET variable and the after get variable manipulation. And always check all the data that is not trusted.


    More on validating data:


    According to one of the comments solution with using urldecode on _GET variable:

    <?php echo date('d-m-Y',strtotime(urldecode($_GET["thedate"])));?> 
    

    "Warning! The superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results."

    Source: http://php.net/manual/en/function.urldecode.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员