-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimestampXMLwriter.php
More file actions
78 lines (71 loc) · 2.7 KB
/
TimestampXMLwriter.php
File metadata and controls
78 lines (71 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* TimestampXMLwriter.php
*
* Create XML documents containing timestamp elements
* Extends the XMLWriter PHP extension
*
* Allows returning XML document as string, saving to a file, or can output
* document with correct XML headers
*
*/
require_once 'math_functions.php';
class TimestampXMLWriter extends XMLWriter
{
// DateTime object created in Constructor
private $date;
// Switch for ignoring prime years - disabled by default
public $ignore_prime_years = false;
// Configure XMLWriter on instantiation
public function __construct() {
$this->date = new DateTime;
$this->openMemory();
$this->setIndent(true);
$this->setIndentString(' ');
$this->startDocument('1.0', 'UTF-8');
$this->startElement('timestamps');
}
// Set timezone to be used in calls to addTimestamp
public function setTimezone($timezone) {
$this->date->setTimezone(new DateTimeZone($timezone));
}
// Add <stimestamp> element to XML document
public function addTimestamp($timestamp) {
// Set DateTime object to current timestamp
$this->date->setTimestamp($timestamp);
if ($this->ignore_prime_years) {
// Skip if year is prime
if ($this->isYearPrime()) return;
}
// Create timestamp XML element with time & text attributes
$this->startElement('timestamp');
$this->writeAttribute('time', $timestamp);
$this->writeAttribute('text', $this->makeDate($timestamp));
$this->endElement();
}
// Return the XML document as a string
public function getDocument() {
$this->endElement();
$this->endDocument();
return $this->outputMemory();
}
// Output XML document with headers
// Do not call if output already sent
public function outputDocument() {
header('Content-type: text/xml');
echo $this->getDocument();
}
// Write XML document to specified filename
public function saveDocument($file) {
return file_put_contents($file, $this->getdocument());
}
// Return current DateTime as a string
protected function makeDate($timestamp) {
return $this->date->format('Y-m-d H:i:s');
}
// Return true if year is a prime number, else false
protected function isYearPrime() {
return is_prime($this->date->format('Y'));
}
}
?>