reference/datetime/functions/strtotime.xml
5c951013ca04161992efed8b86fb40f55669958e
...
...
@@ -9,17 +9,27 @@
9
9
<refsect1 role="description">
10
10
&reftitle.description;
11
11
<methodsynopsis>
12
-
<type>int</type><methodname>strtotime</methodname>
13
-
<methodparam><type>string</type><parameter>time</parameter></methodparam>
14
-
<methodparam choice="opt"><type>int</type><parameter>now</parameter><initializer>time()</initializer></methodparam>
12
+
<type class="union"><type>int</type><type>false</type></type><methodname>strtotime</methodname>
13
+
<methodparam><type>string</type><parameter>datetime</parameter></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>
26
+
<warning>
27
+
<para>
28
+
The Unix timestamp that this function returns does not contain information
29
+
about time zones. In order to do calculations with date/time information,
30
+
you should use the more capable <classname>DateTimeImmutable</classname>.
31
+
</para>
32
+
</warning>
23
33
<para>
24
34
Each parameter of this function uses the default time zone unless a
25
35
time zone is specified in that parameter. Be careful not to use
...
...
@@ -34,13 +44,13 @@
34
44
<para>
35
45
<variablelist>
36
46
<varlistentry>
37
-
<term><parameter>time</parameter></term>
47
+
<term><parameter>datetime</parameter></term>
38
48
<listitem>
39
49
<para>&date.formats.parameter;</para>
40
50
</listitem>
41
51
</varlistentry>
42
52
<varlistentry>
43
-
<term><parameter>now</parameter></term>
53
+
<term><parameter>baseTimestamp</parameter></term>
44
54
<listitem>
45
55
<para>
46
56
The timestamp which is used as a base for the calculation of relative
...
...
@@ -55,8 +65,7 @@
55
65
<refsect1 role="returnvalues">
56
66
&reftitle.returnvalues;
57
67
<para>
58
-
Returns a timestamp on success, &false; otherwise. Previous to PHP 5.1.0,
59
-
this function would return <literal>-1</literal> on failure.
68
+
Returns a timestamp on success, &false; otherwise.
60
69
</para>
61
70
</refsect1>
62
71

...
...
@@ -80,56 +89,9 @@
80
89
</thead>
81
90
<tbody>
82
91
<row>
83
-
<entry>5.3.0</entry>
84
-
<entry>
85
-
Prior to PHP 5.3.0, relative time formats supplied to the
86
-
<parameter>time</parameter> argument of <function>strtotime</function>
87
-
such as <literal>this week</literal>, <literal>previous week</literal>,
88
-
<literal>last week</literal>, and <literal>next week</literal> were
89
-
interpreted to mean a 7 day period relative to the current date/time, rather
90
-
than a week period of <literal>Monday</literal> through <literal>Sunday</literal>.
91
-
</entry>
92
-
</row>
93
-
<row>
94
-
<entry>5.3.0</entry>
95
-
<entry>
96
-
Prior to PHP 5.3.0, <literal>24:00</literal> was not a valid format and
97
-
<function>strtotime</function> returned &false;.
98
-
</entry>
99
-
</row>
100
-
<row>
101
-
<entry>5.2.7</entry>
92
+
<entry>8.0.0</entry>
102
93
<entry>
103
-
In PHP 5 prior to 5.2.7, requesting a given occurrence of a
104
-
given weekday in a month where that weekday was the first day
105
-
of the month would incorrectly add one week to the returned
106
-
timestamp. This has been corrected in 5.2.7 and later
107
-
versions.
108
-
</entry>
109
-
</row>
110
-
<row>
111
-
<entry>5.1.0</entry>
112
-
<entry>
113
-
Now returns &false; on failure, instead
114
-
of <literal>-1</literal>.
115
-
</entry>
116
-
</row>
117
-

118
-
&date.timezone.errors.changelog;
119
-

120
-
<row>
121
-
<entry>5.0.2</entry>
122
-
<entry>
123
-
In PHP 5 up to 5.0.2, <literal>"now"</literal> and other
124
-
relative times are wrongly computed from today's
125
-
midnight. This differs from other versions where it is
126
-
correctly computed from current time.
127
-
</entry>
128
-
</row>
129
-
<row>
130
-
<entry>5.0.0</entry>
131
-
<entry>
132
-
Microseconds began to be allowed, but they are ignored.
94
+
<parameter>baseTimestamp</parameter> is nullable now.
133
95
</entry>
134
96
</row>
135
97
</tbody>
...
...
@@ -166,7 +128,6 @@ echo strtotime("last Monday"), "\n";
166
128
<?php
167
129
$str = 'Not Good';
168
130

169
-
// previous to PHP 5.1.0 you would compare with -1, instead of false
170
131
if (($timestamp = strtotime($str)) === false) {
171
132
echo "The string ($str) is bogus";
172
133
} else {
...
...
@@ -183,6 +144,18 @@ if (($timestamp = strtotime($str)) === false) {
183
144
&reftitle.notes;
184
145
<note>
185
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>
186
159
If the number of the year is specified in a two digit format, the values
187
160
between 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999. See the notes
188
161
below for possible differences on 32bit systems (possible dates might end on
...
...
@@ -197,12 +170,6 @@ if (($timestamp = strtotime($str)) === false) {
197
170
a 32-bit signed integer.)
198
171
</para>
199
172
<para>
200
-
Prior to PHP 5.1.0, not all platforms support negative timestamps, therefore
201
-
your date range may be limited to no earlier than the Unix epoch. This
202
-
means that e.g. dates prior to Jan 1, 1970 will not work on Windows,
203
-
some Linux distributions, and a few other operating systems.
204
-
</para>
205
-
<para>
206
173
For 64-bit versions of PHP, the valid range of a timestamp is effectively
207
174
infinite, as 64 bits can represent approximately 293 billion years in either
208
175
direction.
...
...
@@ -210,28 +177,9 @@ if (($timestamp = strtotime($str)) === false) {
210
177
</note>
211
178
<note>
212
179
<para>
213
-
Dates in the <literal>m/d/y</literal> or <literal>d-m-y</literal> formats
214
-
are disambiguated by looking at the separator between the various
215
-
components: if the separator is a slash (<literal>/</literal>), then the
216
-
American <literal>m/d/y</literal> is assumed; whereas if the separator is a
217
-
dash (<literal>-</literal>) or a dot (<literal>.</literal>), then the
218
-
European <literal>d-m-y</literal> format is assumed.
219
-
If, however, the year is given in a two digit format and the separator is a
220
-
dash (<literal>-</literal>, the date string is parsed as
221
-
<literal>y-m-d</literal>.
222
-
</para>
223
-
<para>
224
-
To avoid potential ambiguity, it's best to use ISO 8601
225
-
(<literal>YYYY-MM-DD</literal>) dates or
226
-
<methodname>DateTime::createFromFormat</methodname> when possible.
227
-
</para>
228
-
</note>
229
-
<note>
230
-
<para>
231
180
Using this function for mathematical operations is not advisable.
232
181
It is better to use <methodname>DateTime::add</methodname> and
233
-
<methodname>DateTime::sub</methodname> in PHP 5.3 and later, or
234
-
<methodname>DateTime::modify</methodname> in PHP 5.2.
182
+
<methodname>DateTime::sub</methodname>.
235
183
</para>
236
184
</note>
237
185
</refsect1>
...
...
@@ -240,15 +188,15 @@ if (($timestamp = strtotime($str)) === false) {
240
188
&reftitle.seealso;
241
189
<para>
242
190
<simplelist>
191
+
<member><classname>DateTimeImmutable</classname></member>
192
+
<member><methodname>DateTimeImmutable::createFromFormat</methodname></member>
243
193
<member><link linkend="datetime.formats">Date and Time Formats</link></member>
244
-
<member><methodname>DateTime::createFromFormat</methodname></member>
245
194
<member><function>checkdate</function></member>
246
195
<member><function>strptime</function></member>
247
196
</simplelist>
248
197
</para>
249
198
</refsect1>
250
199
</refentry>
251
-

252
200
<!-- Keep this comment at the end of the file
253
201
Local variables:
254
202
mode: sgml
255
203