将java纪元字符串与php纪元日期进行比较
这里是我有一个
java字符串纪元日期,我想比较和添加数据
SQL它给出一个解析错误,不知道为什么有任何解决方案.. <?php if($_SERVER['REQUEST_METHOD']=='POST'){ //Getting values $number = $_POST['number']; $type = $_POST['type']; $date = $_POST['date']; $content = $_POST['content']; $start = strtotime("now"); $end = strtotime("-7 days"); while($start->format('U') > $date->format('U') > $end->format('U')){ $sql = "INSERT INTO message_detail (number,type,date,content) VALUES ('$number','$type','$date','$content')"; //Importing our db connection script require_once('connect.php'); //Executing query to database if(mysqli_query($con,$sql)){ echo 'Entry Added Successfully'; }else{ echo 'Could Not Add Entry'; } } //Closing the database mysqli_close($con); } ?> 解决方法
您的代码几乎没有问题,例如:
> $start = strtotime("now"); $start->format('U'); // etc. are wrong 相反,创建一个DateTime对象,然后调用它的format()方法,如下所示: $start = new DateTime("now"); $start->format('U'); // etc. >现在谈谈你的问题,
那是因为你的条件, while($start->format('U') > $date->format('U') > $end->format('U')){ ... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ?可能是,你想做这样的事情: while($start->format('U') < $date->format('U') && $date->format('U') > $end->format('U')){ // your code } 图片的标题说明: >在循环的每次迭代中都不包括连接处理程序,所以请使用此语句require_once(‘connect.php’);在你的while循环之外. 编辑:
如果这是你的要求,那么你可以使用strtotime()函数或DateTime类来做到这一点, (1)使用strtotime()函数 // your code $content = $_POST['content']; $start = strtotime("-7 days"); $date = strtotime($date); $end = strtotime("now"); 并且没有必要使用while循环,一个简单的if条件就可以了, if($start <= $date && $end >= $date){ // your code } (2)使用DateTime类 // your code $content = $_POST['content']; $start = new DateTime("-7 days"); $date = new DateTime($date); $end = new DateTime("now"); 并且没有必要使用while循环, if($start->format('U') <= $date->format('U') && $end->format('U') >= $date->format('U')){ // your code } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |