如何用 UNIX 时间戳替换人类可读的日期字符串?

发布于 2024-12-21 13:07:03 字数 587 浏览 0 评论 0原文

我有一个非常大的 txt 文件 javascript 数组 - 太大了,无法单独编辑每个条目。它看起来像这样:

["12/1/2011 00:00",15848],["12/1/2011 01:00",15108],["12/1/2011 02:00",14643],["12/1/2011 03:00",14265], &c.

我如何通过 PHP 传递这个文件来更改所有日期并写入一个新文件?我想我需要使用 strptime()strtotime() 但我不知道如何继续。

日期格式为月/日/年

编辑:我最终从 CSV 文件重新创建了数组。这是我使用的代码,以防有人感兴趣。感谢您的帮助。

$handle = fopen("stuff.csv", "r");

while(($data = fgetcsv($handle, ",")) !== FALSE) {
    echo "[" . strtotime($data[0]) . ", " . $data[1] . "],<br />";
}

I have a very large txt file javascript array - too big for me to go and edit each entry separately. It looks like this:

["12/1/2011 00:00",15848],["12/1/2011 01:00",15108],["12/1/2011 02:00",14643],["12/1/2011 03:00",14265], &c.

How can I pass this file through PHP to change all the dates and write a new file? I'm thinking I need to use strptime() or strtotime() but I'm not sure how to proceed.

The date format is Month/Day/Year.

EDIT: I ended up recreating the array from a CSV file. Here's the code I used in case anyone's interested. Thanks for the help.

$handle = fopen("stuff.csv", "r");

while(($data = fgetcsv($handle, ",")) !== FALSE) {
    echo "[" . strtotime($data[0]) . ", " . $data[1] . "],<br />";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

☆獨立☆ 2024-12-28 13:07:03

使用正则表达式匹配字符串中的所有日期,然后对结果使用 strtotime():

$str = '["12/1/2011 00:00",15848],["12/1/2011 01:00",15108],["12/1/2011 02:00",14643],["12/1/2011 03:00",14265]';
$p = '#(\d+/\d+/\d{4}\s\d{2}:\d{2})#';
preg_match_all($p, $str, $matches);

foreach ($matches[1] as $m) {
  echo strtotime($m) . "\n";
}

更新: 刚刚意识到您说您的数据位于 JavaScript 数组中。你也可以在 JS 中轻松处理这个问题:

var new_times = [];
var times = [["12/1/2011 00:00",15848],["12/1/2011 01:00",15108],["12/1/2011 02:00",14643],["12/1/2011 03:00",14265]];

for(i=0; i < times.length; i++) {
  var d = new Date(times[i][0]);
  var new_arr = [(d.getTime() / 1000), times[i][1]];
  new_times.push(new_arr);
}

Match all the dates in the string with a regular expression, then use strtotime() on the results:

$str = '["12/1/2011 00:00",15848],["12/1/2011 01:00",15108],["12/1/2011 02:00",14643],["12/1/2011 03:00",14265]';
$p = '#(\d+/\d+/\d{4}\s\d{2}:\d{2})#';
preg_match_all($p, $str, $matches);

foreach ($matches[1] as $m) {
  echo strtotime($m) . "\n";
}

UPDATE: Just realized you said your data is in a javascript array. You can handle this easily in JS as well:

var new_times = [];
var times = [["12/1/2011 00:00",15848],["12/1/2011 01:00",15108],["12/1/2011 02:00",14643],["12/1/2011 03:00",14265]];

for(i=0; i < times.length; i++) {
  var d = new Date(times[i][0]);
  var new_arr = [(d.getTime() / 1000), times[i][1]];
  new_times.push(new_arr);
}
地狱即天堂 2024-12-28 13:07:03

JavasScript

var times = [["12/1/2011 00:00",15848],["12/1/2011 01:00",15108],["12/1/2011 02:00",14643],["12/1/2011 03:00",14265]];
//var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
for(i in times) {
    var time = times[i][0].split(/\/|\s|:/i);
    console.log(time);
    date = new Date(time[2], time[0], time[1], time[3], time[4]);
    console.log(date.getTime());
}

JavasScript

var times = [["12/1/2011 00:00",15848],["12/1/2011 01:00",15108],["12/1/2011 02:00",14643],["12/1/2011 03:00",14265]];
//var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
for(i in times) {
    var time = times[i][0].split(/\/|\s|:/i);
    console.log(time);
    date = new Date(time[2], time[0], time[1], time[3], time[4]);
    console.log(date.getTime());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文