1

从 HTML 表单提交时,我需要向 XML 文件添加时间戳。我有一个 php 脚本,它使用从表单生成的数据创建一个 XML 文件。提交表单时,我希望它在 XML 文件中有一个时间戳元素。最简单的方法是什么?表单中的隐藏字段还是我可以添加到 php 脚本中来为我执行此操作?

这是我使用的创建文件并将表单中的数据插入 XML 文件的脚本。

<?php

if (isset($_POST['lsr-submit']))
    {
        header('Location: http://www.mesquiteweather.net/wxmesqLSR.php');
    }

$str = '<?xml version="1.0" encoding="UTF-8"?><entrys></entrys>';
$xml = simplexml_load_string($str);

$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$location = $_POST['location'];
$report = $_POST['report'];
$description = $_POST['desc'];

$fname = htmlentities($fname, ENT_COMPAT, 'UTF-8', false);
$lname = htmlentities($lname, ENT_COMPAT, 'UTF-8', false);
$location = htmlentities($location, ENT_COMPAT, 'UTF-8', false);
$report = htmlentities($report, ENT_COMPAT, 'UTF-8', false);
$description = htmlentities($description, ENT_COMPAT, 'UTF-8', false);

$xml->reports = "";
$xml->reports->addChild('fname', $fname);
$xml->reports->addChild('lname', $lname);
$xml->reports->addChild('location', $location);
$xml->reports->addChild('report', $report);
$xml->reports->addChild('description', $description);

$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$doc->preserveWhiteSpace = true;
$doc->loadXML($xml->asXML(), LIBXML_NOBLANKS);
$doc->save('test2.xml');

?>

-谢谢!

4

1 回答 1

0

如果用户没有输入数据,则无需在 html 中创建另一个输入字段。您可以将孩子添加到您的 xml 文件中。

添加这一行

$xml->reports->addChild('time', date("Y-m-d H:i:s",time()-60*60));

正如您在评论中建议的那样,这将添加一小时后的日期。您可以更改日期格式以满足您的需要。要找到如何操作,您可以在手册中检查 Date()

于 2013-05-31T06:48:29.120 回答