dongzhi2887 2011-11-07 10:34
浏览 21
已采纳

strtotime日期验证在无效日期传递

I am in the middle of setting up a basic CMS that allows the client to add articles to their mobile app. The CMS is coded in PHP and will use JSON to deliver the content to the mobile app.

Now my problem is there is an option to publish the article at a certain date, so I want to validate the date to check it is valid.

So to test possibilites I made a small script. I am using strtotime() to check the date is valid, my script is:

<?php

    $date[] = '2011-31-01';
    $date[] = '2011-02-31';

    foreach($date as $str) {
        if(strtotime($str) == false) {
            $result[] = '<p>[' . $str . '] Resulted in an <span style="color: red;">Error.</span></p>';
        } else {
            $result[] = '<p>[' . $str . '] Resulted in <span style="color: green;">Success.</span></p>';
        }
    }

    foreach($result as $return) {
        echo $return;
    }

?>

Now my problem is the date 2011-02-31 which is 31st February 2011 is returning as valid, when obviously it isn't. So my question is why does it do this? and is there a better method to check that the date is valid and exists?

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dsf487787 2011-11-07 10:39
    关注

    checkdate(); Validates a Gregorian date. Returns TRUE if the date given is valid; otherwise returns FALSE.

     if(checkdate(2, 31, 2011)){
          echo "Yeah";
    
      } else {echo "nah";}
    

    It returns false!

    That's the way to go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi