javascript - Why does Internet Explorer and Edge output new Date() data into database differently than other browsers?

254

I found a way to add a date as a time stamp when a user submits a message with my web application. Every thing works perfectly but when I usenew Date(); to have its value store in a MySQL database, Internet Explorer and Edge just inputs the data like this ‎12‎:‎14‎ ‎PM but it suppose to look something like this 12:33 PM.

In other words different browsers input it perfectly in the database so I know my code isn't wrong but Edge and Explorer don't store the data properly so what's going on here? And how can I get Edge and Explorer to store the time data like how the other browsers do perfectly? And this is a screenshot of the database.

Screenshot

The method I'm using to store data into the database works perfectly with all different kinds of data like strings, numeric etc.. and all different browsers but edge and explorer can't store JavaScript new Date() value properly.

Here is a method on how I'm getting a time stamp and generating values to be use in the server side code.

message.js

//Get message date
function message_date() {

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

if(dd<10) {
    dd = '0'+dd
} 

if(mm<10) {
    mm = '0'+mm
} 

today = mm + '/' + dd + '/' + yyyy;
var today;  
return today;
}  

var message_date_stamp = message_date();  // this will grab you the return value from message_date();

//Get user send message time
var timez = new Date();
timez = timez.toLocaleString('en-US', { hour: 'numeric',minute:'numeric', hour12: true });
var get_time = timez;

$(document).ready(function(){  
$("#send").click(function(){
var conversation_id = $("#conversation_id").val();
var user_id = $("#user_id").val();
var member_name = $("#member_name").val();
var message = $("#message").val();
var status= $("#status").val();
var sender_id= $("#sender_id").val();
var receiver_id= $("#receiver_id").val();
var photo_link = $("#photo_link").val();
var hyper_link = $("#hyper_link").val();
var date = message_date_stamp;
var time = get_time;

// Returns successful data submission message when the entered information is stored in database.
var dataString = 'conversation_id='+ conversation_id + '&user_id=' + user_id + '&member_name='+ member_name + '&message='+ message + '&status='+ status + '&sender_id='+ sender_id + '&receiver_id='+ receiver_id + '&photo_link='+ photo_link + '&hyper_link='+ hyper_link + '&time='+ time + '&date='+ date;

    //AJAX code to submit form.
    $.ajax({
    type: "POST",
    url: "send_message_data.php",
    data: dataString
    });
    });
    });

This is how I'm storing the data into the database in PHP just know that the value of new Date() is generated from a previous page and in PHP it's represented by PHP's variable call $time.

send_message_data.php

<?php
//Credentials
$db_servername = "localhost";
$db_username = "1234u";
$db_password = "1234";
$db_name = "1234db";

$connect = mysqli_connect($db_servername, $db_username, $db_password, $db_name);

//Form values
$conversation_id = $_POST['conversation_id'];
$user_id = $_POST['user_id'];
$member_name= $_POST['member_name'];
$message= $_POST['message'];
$status= $_POST['status'];
$sender_id= $_POST['sender_id'];
$receiver_id= $_POST['receiver_id'];

$date= $_POST['date'];

//FOR STACK OVER FLOW USERS $time is how I'm storing this into the data base  
$time= $_POST['time'];


$photo_link= $_POST['photo_link'];
$hyper_link= $_POST['hyper_link'];

//SECURITY

//Basic character escaping.
$conversation_id = htmlspecialchars($conversation_id);
$member_name = htmlspecialchars($member_name);
$message = htmlspecialchars($message);
$photo_link = htmlspecialchars($photo_link);
$hyper_link = htmlspecialchars($hyper_link);

//OTHER FEATURES 




//Prevent MYSQLI injection.
$conversation_id = mysqli_real_escape_string($connect, $conversation_id);
$member_name = mysqli_real_escape_string($connect, $member_name);
$message = mysqli_real_escape_string($connect, $message);
$photo_link = mysqli_real_escape_string($connect, $photo_link );
$hyper_link = mysqli_real_escape_string($connect, $hyper_link);

//Removes tags combat the following injections HTML, JAVA-SCRIPT, PHP.
$conversation_id = filter_var($conversation_id, FILTER_SANITIZE_STRING);
$member_name = filter_var($member_name, FILTER_SANITIZE_STRING);
$message = filter_var($message, FILTER_SANITIZE_STRING);
$photo_link = filter_var($photo_link, FILTER_SANITIZE_STRING);
$hyper_link = filter_var($hyper_link, FILTER_SANITIZE_STRING);


$query = "INSERT INTO messages_x1(conversation_id,user_id,member_name,message,status,sender_id,receiver_id,photo_link,hyper_link,date,time) values ('$conversation_id','$user_id','$member_name','$message','$status','$sender_id','$receiver_id','$photo_link','$hyper_link','$date','$time')";

$run = $connect->query($query);

if($run) {
echo "Form Submitted succesfully";
}

?>

And one last thing I read that it has nothing to do with the code but I read some where that mentions Explorer and Edge have problems on interacting with JavaScript'snew Date() poorly in certain situations. I just want a solution to bypass Explorer and Edge limitations.

903

Answer

Solution:

No offense to the people that said it was my code that had errors. But I always knew it wasn't my code that was causing this. I was right I found out that different browsers will tend to structure your code differently in your database. So for any future readers wondering why their code is treated and inserted differently in the data base then you know why. Here is proof starting from internet explore to chrome and to safari Iphone.

Screen shot

The good news is that internet explore version of the time is outputting the time in a human readable way but sadly it's structure of the time differs in the data base.

People are also looking for solutions to the problem: php - How I have to encode url for Facebook sharing

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.