1

I have a database in mysql named apply(id, user, time_apply). I have set structure for time_apply to "datetime"

I have a php script which is trying to get current DATE and TIME from a user's pc and store it my database. User's pc can be anywhere in the world.

<?php

$timeString='
<script language="javascript">
var today = new Date();
document.write(today);
</script> ';

//id is my database primary key, so I don't insert anything inside

mysql_query("INSERT INTO `apply` VALUES ('', '$session_user_id', '$timeString') "); 


?>

The problem is that after mysql_query executed, I cannot insert current user's date and time in my database. I get something like : 0000-00-00 00:00:00

Any idea how to fix it?

user2491321
  • 673
  • 10
  • 32
  • Instead of adding the data through PHP, you can declare the field (time_apply) as TIMESTAMP, and attribute as ON UPDATE CURRENT TIMESTAMP. So that, on adding/pdating the data, the field will have the updated current time. – prava May 29 '14 at 17:00
  • 2
    Mixing PHP and JavaScript. The JavaScript is run on client-side... by that time you have already executed the SQL query. You need to post back the date to PHP and then store in DataBase. – Nawed Khan May 29 '14 at 17:00
  • @Prava the OP wants to store the client's PC datetime and not his own server DATATIME. – Nawed Khan May 29 '14 at 17:01
  • You need to use AJAX or have Javascript add the time to a hidden field in a form. – Barmar May 29 '14 at 17:02
  • Why is the time on the PC different from the time on the server? Everyone should run time synchronization software these days. – Barmar May 29 '14 at 17:03
  • The only difference would be the time zone. Is that what you're trying to get? – Barmar May 29 '14 at 17:04
  • yes...because my server will be in usa and a user can be in uk, so there will be difference in time. – user2491321 May 29 '14 at 17:05
  • If you're trying to get the current time, why does it matter that you get it from the User's PC? The Timezones will be different but that also means the User could change their clock to submit any time they want to you. – Oscar M. May 29 '14 at 18:52

4 Answers4

1

ugly coding (but this is what you wanted ) :)

$timeString='
<script language="javascript">
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 + " " +today.getHours() + ":" + today.getMinutes()+":" + today.getSeconds();
document.write(today);
</script> ';


echo "INSERT INTO `apply` VALUES ('test', '1', '$timeString') ";

output

INSERT INTO `apply` VALUES ('test', '1', ' 05/29/2014 22:50:4 ')
ɹɐqʞɐ zoɹǝɟ
  • 4,342
  • 3
  • 22
  • 35
  • looks great... A final question please. This takes the user's PC date time right? Because I want to get the user's date-time not my server's. I was using now() in php before, but that was using server's time. – user2491321 May 29 '14 at 17:32
  • yes,here we are taking the users system time by javascript and not the server time – ɹɐqʞɐ zoɹǝɟ May 29 '14 at 17:37
1

I don't know why you would want to capture the client's time but here's how you would do it.

First you'll need to separate your javascript and php script. You can have php output javascript but not the way you have it written.

You can create a hidden element that gets set whenever the javascript event of your choice fires. I used the onclick event of the submit button you could use page_load or whatever. You could then pull that value from the php $_POST variables collection.

<script language="javascript">
function submit_OnClick() {
    document.getElementById("clientDate").value =  currentdate.getDate() + "/"
            + (currentdate.getMonth()+1)  + "/" 
            + currentdate.getFullYear() + " @ "  
            + currentdate.getHours() + ":"  
            + currentdate.getMinutes() + ":" 
            + currentdate.getSeconds();
     }
</script> 


<?php

$timeString=$_POST["clientDate"];
mysql_query("INSERT INTO Apply (id, user, time_apply) VALUES ('', '$session_user_id', '$timeString') "); 

?>

<form method="post">
    <input type='hidden' id='clientDate' />
    <input type="submit" value="submit" id='submitForm' OnClick="submit_OnClick();"/>
    <!-- The rest of your form goes here -->
</form>

See this related post for more options and analysis into javascript dates:

Getting current date and time in JavaScript

Community
  • 1
  • 1
GVIrish
  • 361
  • 1
  • 6
0

You have a complete misunderstanding of how PHP and JS operate. You do not have any actual JS code in your snippet above. You have a PHP STRING which happens to contain some text that LOOKS like Javascript.

But since PHP has absolutely NO clue as to what JS is, let alone execute it, you're literally stuffing your JS code into MySQL as a date value. MySQL is of course saying "what the heck is this" and trashing it to the default 0000-00-00 "bad date provided" value.

Remember: PHP executes on the server, JS executes on the client.

There is absolutely NO reason to have a client generate dates for use in your server-side DB, when the server-side DB is perfectly capable of generating its own dates:

INSERT ... VALUES(..., now());
                       ^^^^^---literally all you need.
Marc B
  • 356,200
  • 43
  • 426
  • 500
0

1) Instead of adding the data through PHP, you can declare the field (time_apply) as TIMESTAMP, and attribute as ON UPDATE CURRENT TIMESTAMP. So that, on adding/pdating the data, the field will have the updated current time.

2) Or else you can use $timeString = date('Y-m-d H:i:s'); value for inserting current time.

3) Or else you can use NOW() method for the field for the value while writing the script.

prava
  • 3,916
  • 2
  • 24
  • 35