Generate txt file with updated current time every second with php and ajax

Asked

Viewed 513 times

0

I have in the company a desktop software that receives information from text files (txt) generated by a web page on php. However, I need to make the software read (in real time) the date and time of the system, and each information must be generated in a different file. I got through the php generate the date and time according to the click of a button on the page, but it needed the time to be updated in real time so that the program can display correctly. Is there a function that passes and updates a txt file to the current time? We can use php, js, ajax, jquery or any other alternative. Another option would be to create a file xml that would receive, in real time, the current time of the system, but I have no idea how to do this.

Follow the code used to record the date and time in the files:

<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <meta charset="utf-8">
        <title>Gestão para OBS</title>
    </head>

    <body>
        <form action="index.php" method="post">
            <input type="submit" value="Data" name="btnBotao"> 
            <input type="submit" value="Hora" name="btnBotao">
        </form>
    </body>

    <?php
        $botao = $_POST["btnBotao"];
        switch($botao)
        {
            case 'Data':
                date_default_timezone_set('America/Sao_Paulo');
                $data = date("d/m/Y");
                $arquivo_data = "Data.txt";
                $fo = fopen($arquivo_data, "w+");
                fwrite($fo, $data);
                fclose($fo);
                break;

            case 'Hora':
                date_default_timezone_set('America/Sao_Paulo');
                $hora = date("H:i");
                $arquivo_hora = "Hora.txt";
                $fo = fopen($arquivo_hora, "w+");
                fwrite($fo, $hora);
                fclose($fo);
                break;              
        }
    ?>
</html>

The program generates the files Data.txt and Hora.txt, but the time needs to be updated in real time.

  • I think your question got a little confused, you want to keep updating a txt file every second or you want your inupts to take the date and time at the click of the button?

  • Your input date and time are with the same name, then the program will never enter the section Hora of your case.

  • Utilize setInterval (js), but PHP is not the best solution. Maybe Nodejs is better for you.

  • Exactly Leandro. What I need is for a txt file to be updated according to the current time.

  • Valdeir, I’ll take a look at Nodejs. I’ve never worked with him but I’ll check. Thank you

  • Look about task scheduler, you don’t need to switch technology to do this. If your server is windows, the tool is really called Agendador de Tarefas. If it is linux this tool is the cron. You schedule a script to run from time to time.

  • Roberto, the problem with using the task scheduler or cron is that it is platform specific. Our software runs on both Windows and Linux environments, and using a web page to generate the files, all stations could access the file path that is on our server to use the date and time in the software. So we need something that works via the web, whether in php, javascript, c#, jquery or any other suggestion.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.