reference/datetime/datetimeinterface/gettimestamp.xml
71692b6f4cace8dca72a18ccd80d4cd7305e5d4e
...
...
@@ -1,11 +1,10 @@
1
1
<?xml version="1.0" encoding="utf-8"?>
2
2
<!-- $Revision$ -->
3
-

4
3
<refentry xml:id="datetime.gettimestamp" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
5
4
<refnamediv>
6
-
<refname>DateTime::getTimestamp</refname>
7
-
<refname>DateTimeImmutable::getTimestamp</refname>
8
5
<refname>DateTimeInterface::getTimestamp</refname>
6
+
<refname>DateTimeImmutable::getTimestamp</refname>
7
+
<refname>DateTime::getTimestamp</refname>
9
8
<refname>date_timestamp_get</refname>
10
9
<refpurpose>Gets the Unix timestamp</refpurpose>
11
10
</refnamediv>
...
...
@@ -13,20 +12,20 @@
13
12
<refsect1 role="description">
14
13
&reftitle.description;
15
14
<para>&style.oop;</para>
16
-
<methodsynopsis role="oop">
17
-
<modifier>public</modifier> <type>int</type><methodname>DateTime::getTimestamp</methodname>
18
-
<void />
15
+
<methodsynopsis role="DateTimeInterface">
16
+
<modifier>public</modifier> <type>int</type><methodname>DateTimeInterface::getTimestamp</methodname>
17
+
<void/>
19
18
</methodsynopsis>
20
19
<methodsynopsis role="DateTimeImmutable">
21
20
<modifier>public</modifier> <type>int</type><methodname>DateTimeImmutable::getTimestamp</methodname>
22
-
<void />
21
+
<void/>
23
22
</methodsynopsis>
24
-
<methodsynopsis role="DateTimeInterface">
25
-
<modifier>public</modifier> <type>int</type><methodname>DateTimeInterface::getTimestamp</methodname>
26
-
<void />
23
+
<methodsynopsis role="DateTime">
24
+
<modifier>public</modifier> <type>int</type><methodname>DateTime::getTimestamp</methodname>
25
+
<void/>
27
26
</methodsynopsis>
28
27
<para>&style.procedural;</para>
29
-
<methodsynopsis role="procedural">
28
+
<methodsynopsis>
30
29
<type>int</type><methodname>date_timestamp_get</methodname>
31
30
<methodparam><type>DateTimeInterface</type><parameter>object</parameter></methodparam>
32
31
</methodsynopsis>
...
...
@@ -47,6 +46,48 @@
47
46
</para>
48
47
</refsect1>
49
48

49
+
<refsect1 role="errors">
50
+
&reftitle.errors;
51
+
<para>
52
+
If the timestamp cannot be represented as &integer;, a
53
+
<exceptionname>DateRangeError</exceptionname> is thrown. Prior to PHP
54
+
8.3.0, a <exceptionname>ValueError</exceptionname> is thrown. And, prior to
55
+
PHP 8.0.0, &false; was returned in this case. Still, the timestamp can be
56
+
retrieved as &string; by using
57
+
<methodname>DateTimeInterface::format</methodname> with the
58
+
<literal>U</literal> format.
59
+
</para>
60
+
</refsect1>
61
+

62
+
<refsect1 role="changelog"><!-- {{{ -->
63
+
&reftitle.changelog;
64
+
<informaltable>
65
+
<tgroup cols="2">
66
+
<thead>
67
+
<row>
68
+
<entry>&Version;</entry>
69
+
<entry>&Description;</entry>
70
+
</row>
71
+
</thead>
72
+
<tbody>
73
+
<row>
74
+
<entry>8.3.0</entry>
75
+
<entry>
76
+
The out-of-range exception is now
77
+
<exceptionname>DateRangeError</exceptionname>.
78
+
</entry>
79
+
</row>
80
+
<row>
81
+
<entry>8.0.0</entry>
82
+
<entry>
83
+
These functions no longer return &false; on failure.
84
+
</entry>
85
+
</row>
86
+
</tbody>
87
+
</tgroup>
88
+
</informaltable>
89
+
</refsect1><!-- }}} -->
90
+

50
91
<refsect1 role="examples">
51
92
&reftitle.examples;
52
93
<example>
...
...
@@ -55,7 +96,7 @@
55
96
<programlisting role="php">
56
97
<![CDATA[
57
98
<?php
58
-
$date = new DateTime();
99
+
$date = new DateTimeImmutable();
59
100
echo $date->getTimestamp();
60
101
?>
61
102
]]>
...
...
@@ -76,27 +117,45 @@ echo date_timestamp_get($date);
76
117
]]>
77
118
</screen>
78
119
</example>
79
-
</refsect1>
80
-

81
-
<refsect1 role="notes">
82
-
&reftitle.notes;
83
120
<para>
84
-
Using <literal>U</literal> as the parameter to
85
-
<function>DateTime::format</function>
86
-
is an alternative when using PHP 5.2.
121
+
If you need to retrieve the timestamp with millisecond or
122
+
microsecond resolution, then you can use the
123
+
<function>DateTimeInterface::format</function> function.
87
124
</para>
125
+
<example>
126
+
<title>Retrieving timestamp with milli and microsecond resolution</title>
127
+
<para>&style.oop;</para>
128
+
<programlisting role="php">
129
+
<![CDATA[
130
+
<?php
131
+
$date = new DateTimeImmutable();
132
+
$milli = (int)$date->format('Uv'); // Timestamp in milliseconds
133
+
$micro = (int)$date->format('Uu'); // Timestamp in microseconds
134
+

135
+
echo $milli, "\n", $micro, "\n";
136
+
?>
137
+
]]>
138
+
</programlisting>
139
+
&examples.outputs.similar;
140
+
<screen>
141
+
<![CDATA[
142
+
1674057635586
143
+
1674057635586918
144
+
]]>
145
+
</screen>
146
+
</example>
88
147
</refsect1>
89
148

90
149
<refsect1 role="seealso">
91
150
&reftitle.seealso;
92
151
<simplelist>
93
152
<member><function>DateTime::setTimestamp</function></member>
94
-
<member><function>DateTime::format</function></member>
153
+
<member><function>DateTimeImmutable::setTimestamp</function></member>
154
+
<member><function>DateTimeInterface::format</function></member>
95
155
</simplelist>
96
156
</refsect1>
97
157

98
158
</refentry>
99
-

100
159
<!-- Keep this comment at the end of the file
101
160
Local variables:
102
161
mode: sgml
103
162