从文件输入日期,并将输出转换为文件


date input from file and output conversion to file

我有一个日期文件,格式为yyyy-mm-dd,例如1988-12-27

我想从这个文件中读出相应的日期,格式如1988年12月27日,到另一个文件中。

这是进行转换的示例代码:我只能从文件中读取并将其输出到另一个文件。感谢

$input     = '1988-12-27';
$timestamp = strtotime($input);
$output    = date('dS F, Y', $timestamp);
echo $output;

您正在寻找将日期输出到另一个文件中的方法吗?

给你:http://php.net/manual/de/function.file-put-contents.php

file_put_contents('file.txt', $output, FILE_APPEND);

编辑:阅读几乎完全有效:

我假设你的原始文件是这样的:

Date1
Date2
Date3

查看我的代码:

$content = file_get_contents('origin.txt');
$dates = explode("'n", $content);
foreach($dates as $date) {
    // add your code here and write output to the new file

查看本教程:http://www.tutorialspoint.com/php/php_files.htm

您将需要使用fopen()创建文件句柄,使用fread()从输入中读取,使用fwrite()写入输出

此处回答:

 <?php
$content = file_get_contents('origin.txt');
$dates = explode("'n", $content);
foreach($dates as $date) {
$timestamp = strtotime($date);
 $output    = date('dS F, Y', $timestamp);
$myFile = "output.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $output."'n";
fwrite($fh, $stringData);
}
?>