2

I am trying to add parameter in query which passes indian time zone to mysql database server.But when i am passing this query it shows 0000-00-00 00:00:00 in my table. I am using hostinger php my admin database server. Please help me to solve this.

<?php


include 'confi.php';

 date_default_timezone_set('Indian/Maldives');

    $email = isset($_POST['email']) ? mysqli_real_escape_string($conn,$_POST['email']) : "";
    $today = date("d/m/Y h:i:s A");

    $sql ="INSERT INTO `i_order` (`ID`, `email`,`date`) VALUES (NULL, '$email', '$today');"; 

    $qur = mysqli_query($conn,$sql);
    if($qur){
        $json = array("status" => 1, "msg" => "success!");
    }else{
        $json = array("status" => 0, "msg" => "error!");
    }

@mysqli_close($conn);

/* Output header */
    header('Content-type: application/json');
    echo json_encode($json);


?>
LF00
  • 27,015
  • 29
  • 156
  • 295
dev
  • 69
  • 8

2 Answers2

1

To store a date to MySQL it needs to be in a specific format i.e.

YYYY-MM-DD HH:MM:SS

So if you change your format from

$today = date("d/m/Y h:i:s A");

To

$today = date("Y-m-d h:i:s");

The date will store correctly.

The AM and PM part is irrelevant if you are storing a 24 hrs time to thedatabase, you might add that if you were presenting the time to a user, when you read the data back from the database though

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
-1
<?php 

 date_default_timezone_set('Indian/Maldives');
 //echo $today = date('m-d-Y h:i:s A');

?>
RïshïKêsh Kümar
  • 4,734
  • 1
  • 24
  • 36