I have a raspian hotspot running apache2 which does not have an internet connection. It is hosting a web page to control the application it is running, and I would like to add a "Set Date" button. I have added the apache "www-data" user to the sudoers (I know it sounds risky but the whole thing is stand-alone in the middle of a field). In fact my interface to the app (which must run as root) is via a file in /var/tmp, and a script to send SIGUSR1 to the app when it is changed. So the question is merely how to transfer the date from a device (Laptop, iPad, iPhone, etc.) which is accessing the hosted web php page ? I can of course ssh into it and set it from the command line, but I am trying to automate it a bit. Many thanks.
May I ask why you can't just set the date on the device, and leave it be? Unless you're placed in a very specific geolocation, a set date shouldn't differ between the device and the user, should it?
After a power outage the raspi continues from the time it went off. It is quite easy, therefore, to calculate the length of the outage but it does need to be corrected. It is quite easy via ssh, but this way it is all in one place. There are many people wanting to display the client time on their web pages, but I found the answers difficult to understand, so here is my working solution: Firstly you need an html input field, which can be hidden if you like, without a value setting. Secondly you need a bit of javascript to obtain the client date and time: <input type="submit" name="dset-submit" value="Set Date" > // a button to activate the process <input id="tdate" size="30" type="text" name="tdate" > // input field <script type="text/javascript"> var now = new Date(); document.getElementById('tdate').value = now.toUTCString(); // this is the format required by linux date command </script> Thirdly, and this gave me the most grief, if the input field is part of a form, the script must be inside the form. One helpful respondent somewhere stated that a value field can never be set using scripting - more grief. Then you need some php to extract the date string on the server: <?php if (isset($_POST['dset-submit'])) { $tdate = $_REQUEST['tdate']; } ?> I think this can be located anywhere on the page, but I did not try it inside the form. Finally within some form-processing stuff I added: <?php if ($tdate!="") { $myfile = fopen("/var/tmp/tdate","w"); fwrite($myfile,"$tdate\n"); fclose($myfile); chmod("/var/tmp/tdate",0666); exec( "/var/www/html/sigAFLights", $r ); } ?> 'sigAFLights' send SIGUSR1 to my C++ application on the raspi, so that it only looks in /var/tmp when there is something to do, and when it finds a 'tdate' file it reads it into 'lbuf' and calls system(cmdline) where cmdline is set by sprintf(cmdline,"sudo date -s \"%s\"",lbuf); I hope this is useful to someone, and maybe a expert can simplify it ? (You do have to get the various permissions sorted out, including adding "www-data" to the sudoers list - not a problem in the middle of a field)