加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

将java纪元字符串与php纪元日期进行比较

发布时间:2020-12-15 02:16:31 所属栏目:Java 来源:网络整理
导读:这里是我有一个 java字符串纪元日期,我想比较和添加数据 SQL它给出一个解析错误,不知道为什么有任何解决方案.. ?phpif($_SERVER['REQUEST_METHOD']=='POST'){//Getting values$number = $_POST['number'];$type = $_POST['type'];$date = $_POST['date'];$co
这里是我有一个 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);
}
?>

解决方法

您的代码几乎没有问题,例如:

> strtotime()函数返回一个整数时间戳,而不是DateTime对象,因此你不能像这样调用format()方法.

$start = strtotime("now");
$start->format('U');
// etc. are wrong

相反,创建一个DateTime对象,然后调用它的format()方法,如下所示:

$start = new DateTime("now");
$start->format('U');
// etc.

>现在谈谈你的问题,

it’s giving a parse error

那是因为你的条件,

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循环之外.
>了解prepared statements,因为现在您的查询容易受到SQL注入的影响.另见how you can prevent SQL injection in PHP.

编辑:

i want to enter a data of only last 7 days from entry time…

如果这是你的要求,那么你可以使用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

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读