Compare java epoch string with php era date

here I have a date of the java string era, and I want to compare and add data to SQL that gives a parsing error, does not know why any solution ..

<?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);
}
?>
+4
source share
1 answer

There are several problems in your code, for example:

  • strtotime()the function returns an integer timestamp, not DateTime, so you cannot call the method format()this way.

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

    Instead, create an object DateTimeand then call it format(), for example:

    $start = new DateTime("now");
    $start->format('U');
    // etc.
    
  • Now comes to your problem,

    it gives a parsing error

    What because of your condition while,

    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
    
    }
    

    p >


Sidenotes:

  • , require_once('connect.php'); while.

  • , SQL-. . SQL- PHP.


:

7 ...

, 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 ,

if($start->format('U') <= $date->format('U') &&  $end->format('U') >= $date->format('U')){

    // your code

}
0

Source: https://habr.com/ru/post/1653509/


All Articles