简体   繁体   English

使用javascript / php将系统时间戳保存为.txt文件

[英]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. 我正在使用javascript,需要定期将系统时间戳保存到文本文件中。 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. 我需要在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中。

<?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. 我在同一系统上运行javascript和php。 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. 我对javascript和php的语法非常陌生,因此请明确。 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? 是我使用time()的事实,还是因为我的php代码未运行? 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. 我的印象是,如果php代码正常运行,至少会保存一些内容到我的文本文件中。

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. 为了获得这样的日期格式:“ 2005-10-30 T 10:45 UTC”或“ 2007-11-09 T 11:20 UTC”或“ Sat Jul 23 02:16:57 2005”等等,请使用带有适当参数的date()函数。

M- Month, d- Day (ex: 09), Y- Year, h- hour, i- mins, s- seconds, M-月,d-日(例如09),Y-年,h-小时,i-分钟,s-秒,

You also can have a look here for more details (how to create your timestamp): http://www.php.net/manual/en/function.date.php 您也可以在这里查看更多详细信息(如何创建时间戳): http : //www.php.net/manual/zh/function.date.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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