简体   繁体   中英

Client Time in PHP

PHP, as far as I know, can only find the time on server-side. I want the time to be in client time. My approach was to use Javascript, get the client's time, pass it to PHP. This works, but I need to store this "time" in a log file. Here is my code so far:

JS

var d = new Date();
document.getElementById("timestamp").value = d.getTime() / 1000;

PHP

$datevar = date('d/m/Y', $_POST['timestamp']) . ' at ' . date('h:i', $_POST['timestamp']) . PHP_EOL;

$log_file_name = '1.log'; 
file_put_contents($log_file_name, $variable .":  $variable,  $variable, $datevar"  , FILE_APPEND);

HTML

<form name="contactform" method="post" action="survey.php">
<input type="hidden" id="timestamp" name="timestamp" value="" />
<input name="submit" type="submit" value="Submit"> 
</form>

With the above code, the log file just shows exactly the JS code, not the actual time.

I would like to store the actual client time. Any help will be appreciated. Thank you in advance.

You can try using the javascript code:

Math.round((new Date()).getTime() / 1000);

To get the unix timestamp on the client side and then send it in to your form to be stored in the logged file. You can then use PHP's

strftime("%b, %d, %Y", TIMESTAMP)

Method to convert the submitted unix timestamp submitted from the client's end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM