简体   繁体   中英

save system timestamp to .txt file using javascript/php

I'm using javascript and I need to save the system timestamp to a text file periodically. I have found several other posts about other people having the same problem, but none of the solutions that I have found worked. I need to run my code in chrome.

function recordTone() {
       JQuery.ajax({ url: 'saveTimeStamp.php',
         data: {action: 'save'},
         type: 'post',
         success: function(output) {
                  alert(output);
                  }
        });
      }

At the moment I am calling the above function within a another function that is being called recursively. I know that my problem either begins somewhere in this function or in the php that I am trying to call.

<?php
  function registerTone(){
    if(isset($_POST['action']) && !empty($_POST['action'])) {
      $action = $_POST['action'];
      switch($action) {
        case 'save' : saveTimeStamp();break;
      }
    }
  }

  function saveTimeStamp(){
    $my_file = 'session1';
    $handle = fopen($my_file, 'w') or die('Cannot open file:  '$my_file);
    $data = time();
    fwrite($handle, $data);
    fclose($handle);
  }
?>

I know that my function being called recursively is not the problem because I am getting the output that I expect; I just need to record the timestamp each time the function runs and save it to a text file. I am running the javascript and the php on the same system. I am completely ok with doing this task any other way as long as my recursive function is not affected. I just need to save the timestamps. I'm very new to the syntax of javascript and php so please be explicit. Thank you.

EDIT

Sorry I should have been more clear. Data isn't being saved into my text file. Is the fact that I'm using time() the reason for this or is it because my php code isn't running? I was under the impression that there would at least have been something saved into my text file if the php code was working correctly.

Instead of using

 $data = time();

make sure that you use this:

 $data = date('M-d-Y h:i:s');

In order to get a date format like this: "2005-10-30 T 10:45 UTC" or "2007-11-09 T 11:20 UTC" or "Sat Jul 23 02:16:57 2005" and many more, use date() function with proper params.

M- Month, d- Day (ex: 09), Y- Year, h- hour, i- mins, s- seconds,

You also can have a look here for more details (how to create your timestamp): http://www.php.net/manual/en/function.date.php

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