reference/datetime/functions/strtotime.xml
5c951013ca04161992efed8b86fb40f55669958e
...
...
@@ -9,19 +9,22 @@
9
9
<refsect1 role="description">
10
10
&reftitle.description;
11
11
<methodsynopsis>
12
-
<type>int</type><methodname>strtotime</methodname>
12
+
<type class="union"><type>int</type><type>false</type></type><methodname>strtotime</methodname>
13
13
<methodparam><type>string</type><parameter>datetime</parameter></methodparam>
14
-
<methodparam choice="opt"><type>int</type><parameter>now</parameter><initializer>time()</initializer></methodparam>
14
+
<methodparam choice="opt"><type class="union"><type>int</type><type>null</type></type><parameter>baseTimestamp</parameter><initializer>&null;</initializer></methodparam>
15
15
</methodsynopsis>
16
16
<simpara>
17
17
The function expects to be given a string containing an English date format
18
18
and will try to parse that format into a Unix timestamp (the number of
19
19
seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given
20
-
in <parameter>now</parameter>, or the current time if
21
-
<parameter>now</parameter> is not supplied.
20
+
in <parameter>baseTimestamp</parameter>, or the current time if
21
+
<parameter>baseTimestamp</parameter> is not supplied. The date string parsing
22
+
is defined in <link linkend="datetime.formats">Date and Time Formats</link>, and
23
+
has several subtle considerations. Reviewing the full details there is strongly
24
+
recommended.
22
25
</simpara>
23
26
<warning>
24
-
<para>
27
+
<para>
25
28
The Unix timestamp that this function returns does not contain information
26
29
about time zones. In order to do calculations with date/time information,
27
30
you should use the more capable <classname>DateTimeImmutable</classname>.
...
...
@@ -47,7 +50,7 @@
47
50
</listitem>
48
51
</varlistentry>
49
52
<varlistentry>
50
-
<term><parameter>now</parameter></term>
53
+
<term><parameter>baseTimestamp</parameter></term>
51
54
<listitem>
52
55
<para>
53
56
The timestamp which is used as a base for the calculation of relative
...
...
@@ -62,8 +65,7 @@
62
65
<refsect1 role="returnvalues">
63
66
&reftitle.returnvalues;
64
67
<para>
65
-
Returns a timestamp on success, &false; otherwise. Previous to PHP 5.1.0,
66
-
this function would return <literal>-1</literal> on failure.
68
+
Returns a timestamp on success, &false; otherwise.
67
69
</para>
68
70
</refsect1>
69
71

...
...
@@ -74,6 +76,30 @@
74
76
75
77
</refsect1>
76
78

79
+
<refsect1 role="changelog">
80
+
&reftitle.changelog;
81
+
<para>
82
+
<informaltable>
83
+
<tgroup cols="2">
84
+
<thead>
85
+
<row>
86
+
<entry>&Version;</entry>
87
+
<entry>&Description;</entry>
88
+
</row>
89
+
</thead>
90
+
<tbody>
91
+
<row>
92
+
<entry>8.0.0</entry>
93
+
<entry>
94
+
<parameter>baseTimestamp</parameter> is nullable now.
95
+
</entry>
96
+
</row>
97
+
</tbody>
98
+
</tgroup>
99
+
</informaltable>
100
+
</para>
101
+
</refsect1>
102
+

77
103
<refsect1 role="examples">
78
104
&reftitle.examples;
79
105
<para>
...
...
@@ -102,7 +128,6 @@ echo strtotime("last Monday"), "\n";
102
128
<?php
103
129
$str = 'Not Good';
104
130

105
-
// previous to PHP 5.1.0 you would compare with -1, instead of false
106
131
if (($timestamp = strtotime($str)) === false) {
107
132
echo "The string ($str) is bogus";
108
133
} else {
...
...
@@ -119,6 +144,18 @@ if (($timestamp = strtotime($str)) === false) {
119
144
&reftitle.notes;
120
145
<note>
121
146
<para>
147
+
"Relative" date in this case also means that if a particular component
148
+
of the date/time stamp is not provided, it will be taken verbatim from
149
+
the <parameter>baseTimestamp</parameter>. That is, <code>strtotime('February')</code>,
150
+
if run on the 31st of May 2022, will be interpreted as <literal>31 February 2022</literal>,
151
+
which will overflow into a timestamp on <literal>3 March</literal>.
152
+
(In a leap year, it would be <literal>2 March</literal>.) Using
153
+
<code>strtotime('1 February')</code> or <code>strtotime('first day of February')</code>
154
+
would avoid that problem.
155
+
</para>
156
+
</note>
157
+
<note>
158
+
<para>
122
159
If the number of the year is specified in a two digit format, the values
123
160
between 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999. See the notes
124
161
below for possible differences on 32bit systems (possible dates might end on
...
...
@@ -133,12 +170,6 @@ if (($timestamp = strtotime($str)) === false) {
133
170
a 32-bit signed integer.)
134
171
</para>
135
172
<para>
136
-
Prior to PHP 5.1.0, not all platforms support negative timestamps, therefore
137
-
your date range may be limited to no earlier than the Unix epoch. This
138
-
means that e.g. dates prior to Jan 1, 1970 will not work on Windows,
139
-
some Linux distributions, and a few other operating systems.
140
-
</para>
141
-
<para>
142
173
For 64-bit versions of PHP, the valid range of a timestamp is effectively
143
174
infinite, as 64 bits can represent approximately 293 billion years in either
144
175
direction.
...
...
@@ -146,28 +177,9 @@ if (($timestamp = strtotime($str)) === false) {
146
177
</note>
147
178
<note>
148
179
<para>
149
-
Dates in the <literal>m/d/y</literal> or <literal>d-m-y</literal> formats
150
-
are disambiguated by looking at the separator between the various
151
-
components: if the separator is a slash (<literal>/</literal>), then the
152
-
American <literal>m/d/y</literal> is assumed; whereas if the separator is a
153
-
dash (<literal>-</literal>) or a dot (<literal>.</literal>), then the
154
-
European <literal>d-m-y</literal> format is assumed.
155
-
If, however, the year is given in a two digit format and the separator is a
156
-
dash (<literal>-</literal>), the date string is parsed as
157
-
<literal>y-m-d</literal>.
158
-
</para>
159
-
<para>
160
-
To avoid potential ambiguity, it's best to use ISO 8601
161
-
(<literal>YYYY-MM-DD</literal>) dates or
162
-
<methodname>DateTime::createFromFormat</methodname> when possible.
163
-
</para>
164
-
</note>
165
-
<note>
166
-
<para>
167
180
Using this function for mathematical operations is not advisable.
168
181
It is better to use <methodname>DateTime::add</methodname> and
169
-
<methodname>DateTime::sub</methodname> in PHP 5.3 and later, or
170
-
<methodname>DateTime::modify</methodname> in PHP 5.2.
182
+
<methodname>DateTime::sub</methodname>.
171
183
</para>
172
184
</note>
173
185
</refsect1>
...
...
@@ -176,15 +188,15 @@ if (($timestamp = strtotime($str)) === false) {
176
188
&reftitle.seealso;
177
189
<para>
178
190
<simplelist>
191
+
<member><classname>DateTimeImmutable</classname></member>
192
+
<member><methodname>DateTimeImmutable::createFromFormat</methodname></member>
179
193
<member><link linkend="datetime.formats">Date and Time Formats</link></member>
180
-
<member><methodname>DateTime::createFromFormat</methodname></member>
181
194
<member><function>checkdate</function></member>
182
195
<member><function>strptime</function></member>
183
196
</simplelist>
184
197
</para>
185
198
</refsect1>
186
199
</refentry>
187
-

188
200
<!-- Keep this comment at the end of the file
189
201
Local variables:
190
202
mode: sgml
191
203