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

49
62
<refsect1 role="changelog"><!-- {{{ -->
50
63
&reftitle.changelog;
51
64
<informaltable>
...
...
@@ -58,6 +71,13 @@
58
71
</thead>
59
72
<tbody>
60
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>
61
81
<entry>8.0.0</entry>
62
82
<entry>
63
83
These functions no longer return &false; on failure.
...
...
@@ -76,7 +96,7 @@
76
96
<programlisting role="php">
77
97
<![CDATA[
78
98
<?php
79
-
$date = new DateTime();
99
+
$date = new DateTimeImmutable();
80
100
echo $date->getTimestamp();
81
101
?>
82
102
]]>
...
...
@@ -97,13 +117,41 @@ echo date_timestamp_get($date);
97
117
]]>
98
118
</screen>
99
119
</example>
120
+
<para>
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.
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>
100
147
</refsect1>
101
148

102
149
<refsect1 role="seealso">
103
150
&reftitle.seealso;
104
151
<simplelist>
105
152
<member><function>DateTime::setTimestamp</function></member>
106
-
<member><function>DateTime::format</function></member>
153
+
<member><function>DateTimeImmutable::setTimestamp</function></member>
154
+
<member><function>DateTimeInterface::format</function></member>
107
155
</simplelist>
108
156
</refsect1>
109
157

110
158