©
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
(PHP 4, PHP 5, PHP 7)
strtotime — 将任何英文文本的日期时间描述解析为 Unix 时间戳
$time
[, int $now
= time()
] )
本函数预期接受一个包含美国英语日期格式的字符串并尝试将其解析为
Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数),其值相对于
now
参数给出的时间,如果没有提供此参数则用系统当前时间。
本函数将使用 TZ 环境变量(如果有的话)来计算时间戳。自 PHP 5.1.0 起有更容易的方法来定义时区用于所有的日期/时间函数。此过程在 date_default_timezone_get() 函数页面中有说明。
time
日期/时间字符串。正确格式的说明详见 日期与时间格式。
now
用来计算返回值的时间戳。
成功则返回时间戳,否则返回 FALSE
。在 PHP 5.1.0
之前本函数在失败时返回 -1。
在每 次调用日期/时间函数时,如果时区无效则会引发 E_NOTICE
错误,如果使用系统设定值或 TZ
环境变量,则会引发 E_STRICT
或 E_WARNING
消息。参见
date_default_timezone_set() 。
版本 | 说明 |
---|---|
5.3.0 |
在 PHP 5.3.0 之前, 24:00 不是一个有效的格式,并且 strtotime() 会返回 FALSE 。
|
5.2.7 | In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month would incorrectly add one week to the returned timestamp. This has been corrected in 5.2.7 and later versions. |
5.1.0 |
失败时返回 FALSE ,不再是 -1。
|
5.1.0 |
现在发布 |
5.0.2 | 在 PHP 5 中到 5.0.2 之前,"now" 和其它相对时间从今天午夜起错误计算了。这和正确从当前时间起计算的其它版本不同。 |
5.0.0 | Microseconds began to be allowed, but they are ignored. |
4.4.0 | 在 PHP 4.4 之前,"next" 被错误计算为 +2。对此通常的解决方法是用 "+1"。 |
Example #1 strtotime() 例子
<?php
echo strtotime ( "now" ), "\n" ;
echo strtotime ( "10 September 2000" ), "\n" ;
echo strtotime ( "+1 day" ), "\n" ;
echo strtotime ( "+1 week" ), "\n" ;
echo strtotime ( "+1 week 2 days 4 hours 2 seconds" ), "\n" ;
echo strtotime ( "next Thursday" ), "\n" ;
echo strtotime ( "last Monday" ), "\n" ;
?>
Example #2 失败检查
<?php
$str = 'Not Good' ;
// previous to PHP 5.1.0 you would compare with -1, instead of false
if (( $timestamp = strtotime ( $str )) === false ) {
echo "The string ( $str ) is bogus" ;
} else {
echo " $str == " . date ( 'l dS of F Y h:i:s A' , $timestamp );
}
?>
Note:
如果给定的年份是两位数字的格式,则其值 0-69 表示 2000-2069,70-100 表示 1970-2000。 See the notes below for possible differences on 32bit systems (possible dates might end on 2038-01-19 03:14:07).
Note:
有效的时间戳通常从 Fri, 13 Dec 1901 20:45:54 GMT 到 Tue, 19 Jan 2038 03:14:07 GMT(对应于 32 位有符号整数的最小值和最大值)。不是所有的平台都支持负的时间戳,那么日记范围就被限制为不能早于 Unix 纪元。这意味着在 1970 年 1 月 1 日之前的日期将不能用在 Windows,一些 Linux 版本,以及几个其它的操作系统中。不过 PHP 5.1.0 及更新的版本克服了此限制。
For 64-bit versions of PHP, the valid range of a timestamp is effectively infinite, as 64 bits can represent approximately 293 billion years in either direction.
Note:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
Note:
Using this function for mathematical operations is not advisable. It is better to use DateTime::add() and DateTime::sub() in PHP 5.3 and later, or DateTime::modify() in PHP 5.2.
[#1] me at ramynasr dot com [2015-10-26 21:14:25]
There were a lot of comments about the right date format to parse the standard apache log time properly.
<?php
$string = '[26/Oct/2015:15:42:26 -0500]';
$format = '[d/M/Y:H:i:s T]';
$date = DateTime::createFromFormat($format, $string);
?>
[#2] Mauro [2015-07-15 13:38:03]
you can also use:
<?php
strtotime("+1 minute")
?>
this is useful if used with the function time_sleep_until()
[#3] Andrey Kabakchiev [2015-07-10 12:48:58]
Actually the top-voted comment from 'sam at frontiermedia dot net dot au' is not quite right, as is the documentation Note. The correct assumptions made by strtotime() are:
Forward slashes (/) assume US formatted dates, e.g mm/dd/yy
Periods (.) assume Non-US dates, e.g dd.mm.yy
Hypens (-) assume ISO 8601 (ATOM) formatted dates, e.g yy-mm-dd
[#4] joe at strtotime dot co dot uk [2015-03-16 17:06:28]
A useful testing tool for strtotime() and unix timestamp conversion:
http://strtotime.co.uk/
[#5] gabriel dot rempel at gmail dot com [2015-03-05 13:02:39]
// small function auto detect to convert date
function formatDate($date)
{
if (strpos($date,'/') !== false) :
$date = str_replace('/', '-', $date);
$date = date('Y-m-d h:i:s', strtotime($date));
else :
$date = date('d-m-Y h:i:s', strtotime($date));
$date = str_replace('-', '/', $date);
endif;
return $date;
}
[#6] glilimith [2014-12-19 20:48:26]
0s or 00s for day or month will wrap around to the previous month or year (respectively)
for example,
<?php
echo date('Y-m-d',strtotime('2000-04-00')); // '2000-03-31'
echo date('Y-m-d',strtotime('2000-00-04')); // '1999-12-04'
echo date('Y-m-d',strtotime('2000-00-00')); // '1999-11-30'
echo date('Y-m-d',strtotime('0000-00-00')); // '-0001-11-30'
[#7] Anonymous [2014-03-14 07:21:12]
<?php
# strtotime() function with param "now" work only with UTC
# time zone (GMT) Grinvich, but param "midnight" work with
# local time zone
date_default_timezone_set("Europe/Kiev");
echo "Kiev:".strtotime("now");
echo "<br><br>";
date_default_timezone_set("America/New_York");
echo "New-York:".strtotime("now");
echo "<br><br>";
date_default_timezone_set("UTC");
echo "UTC:".strtotime("now");
echo "<br>==========================<br>";
date_default_timezone_set("Europe/Kiev");
echo "Kiev:".strtotime("midnight");
echo "<br><br>";
date_default_timezone_set("America/New_York");
echo "New-York:".strtotime("midnight");
echo "<br><br>";
date_default_timezone_set("UTC");
echo "UTC:".strtotime("midnight");
echo "<br><br>";
?>
result:
Kiev:1394781415
New-York:1394781415
UTC:1394781415
==========================
Kiev:1394748000
New-York:1394769600
UTC:1394755200
[#8] peixoto at live dot com [2014-03-05 12:23:53]
<?php strtotime('-5 weeks monday') ?>
returns the monday of 5 weeks ago.
[#9] Anonymous [2014-02-23 15:13:30]
Be aware that you cannot rely on this function alone to validate a date, as it will accept insane dates like the 31st of February.
Also, the '... week' functionality by itself may not do what you expect. If used on a Sunday, 'next week' will not return a timestamp of the next Monday, but of the Monday after that. Similarly, a timestamp for the Monday of the current week is returned when 'previous/last week' is used and 'this week' returns a stamp of the Monday of the next week (i.e. the following day). This is not the 'week starts on Sunday' effect, as that would mean all the timestamps returned would have to be on a Sunday and none of them are.
[#10] Christian L. [2013-12-01 20:45:06]
To get the start of the current week this code works fine when the week starts on Sundays.
<?php strtotime('this Monday'); ?>
To get the timestamp of the start of a week starting on Mondays (as defined by ISO 8601) this shoud do the trick.
<?php strtotime('next Monday -1 week'); ?>
[#11] kumar AT swatantra.info Swatantra Kumar [2013-04-19 07:27:26]
The "+1 month" issue with strtotime
===================================
As noted in several blogs, strtotime() solves the "+1 month" ("next month") issue on days that do not exist in the subsequent month differently than other implementations like for example MySQL.
<?php
echo date( "Y-m-d", strtotime( "2009-01-31 +1 month" ) ); // PHP: 2009-03-03
echo date( "Y-m-d", strtotime( "2009-01-31 +2 month" ) ); // PHP: 2009-03-31
?>
<?php
SELECT DATE_ADD( '2009-01-31', INTERVAL 1 MONTH ); // MySQL: 2009-02-28
?>
[#12] me at will morgan dot co dot you kay [2012-09-03 17:01:37]
For negative UNIX timestamps, strtotime seems to return the literal you passed in, or it may try to deduct the number of seconds from today's date.
To work around this behaviour, it appears that the same behaviour as described in the DateTime classes applies:
http://php.net/manual/en/datetime.construct.php
Specifically this line here (in the EN manual):
> The $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).
Therefore strtotime('@-1000') returns 1000 seconds before the epoch.
Hope this helps.
[#13] info at jesperveldhuizen dot com [2012-05-16 14:33:50]
There is a change in the strtotime function...
<?php
echo date('Y', strtotime('0000-00-00 00:00:00'));
?>
[#14] darylblake at gmail dot com [2012-03-08 22:13:57]
I was doing work with cisco phone systems, and thy record their times as NTP Format.
strtotime() doesnt process the whole NTP timestamp, off the bat, so if you want a quick function to work I bashed this together pretty quickly, there are probably more efficent ways of processing this, but it gets the job done. Enjoy.
<?php
//Note: I ran this in PHP 5.3.x
function NTPtoUnixtime($ntptime)
{
$explodedNTPtime = explode(" ", $ntptime,2);
$startOfDay = strtotime("Today");
$timeThroughDay = strtotime($explodedNTPtime[0]) - $startOfDay ;
return strtotime($explodedNTPtime[1])+$timeThroughDay;
}
$ntpTime = "09:49:22.897 NZDT Mon Feb 20 2012"; //NTP time to convert.
echo NTPtoUnixtime($ntpTime);
//produces: 1329684562
// Which equates to: Mon 20 Feb 2012 09:49:22 NZDT GMT+13
?>
[#15] cristinawithout [2012-01-31 21:15:26]
WARNING when using "next month", "last month", "+1 month", "-1 month" or any combination of +/-X months. It will give non-intuitive results on Jan 30th and 31st.
As described at : http://derickrethans.nl/obtaining-the-next-month-in-php.html
<?php
$d = new DateTime( '2010-01-31' );
$d->modify( 'next month' );
echo $d->format( 'F' ), "\n";
?>
In the above, using "next month" on January 31 will output "March" even though you might want it to output "February". ("+1 month" will give the same result. "last month", "-1 month" are similarly affected, but the results would be seen at beginning of March.)
The way to get what people would generally be looking for when they say "next month" even on Jan 30 and Jan 31 is to use "first day of next month":
<?php
$d = new DateTime( '2010-01-08' );
$d->modify( 'first day of next month' );
echo $d->format( 'F' ), "\n";
?>
<?php
$d = new DateTime( '2010-01-08' );
$d->modify( 'first day of +1 month' );
echo $d->format( 'F' ), "\n";
?>
[#16] silveraxe at gmail dot com [2011-10-11 02:01:18]
This date is interpreted as American date (MM/DD/YYYY):
$date = "06/10/2011 14:28"; // Oct 10, 2011
This date is interpreted as European date (DD/MM/YYYY):
$otherDate = "06-10-2011 14:28"; // Jun 10, 2011
That's why you see those differences.
[#17] Michael Muryn (MickoZ) [2011-08-10 10:54:31]
[red.: This is a bug, and should be fixed. I have file an issue]
This comment apply to PHP5+
We can now do thing like this with strtotime:
<?php
$weekMondayTime = strtotime('Monday this week');
?>
However this works based on a week starting Sunday. I do not know if we can tweak this PHP behavior, anyone know?
If you want the timestamp of the start of the ISO Week (i.e. on Monday) as defined by ISO 8601, you can use this one liner:
<?php
$isoWeekStartTime = strtotime(date('o-\\WW')); // {isoYear}-W{isoWeekNumber}
?>
You can also find out the start of week of any time and format it into an ISO date with another one liner like this:
<?php
$isoWeekStartDate = date('Y-m-d', strtotime(date('o-\\WW', $time)));
?>
For more information about ISO-8601 and ISO week date:
http://en.wikipedia.org/wiki/ISO_8601#Week_dates
http://en.wikipedia.org/wiki/ISO_week_date
[#18] Annomoys [2011-06-11 03:43:39]
Strtotime() can be used to loop through date range.
as follows
<?php
$start = strtotime('2009-02-01');
$end = strtotime('2009-03-10');
$date = $start;
while($date < $end)
{
//write your code here
$date = strtotime("+1 day", $date);(counter)
}
?>
[#19] a dot fruchi at bit-runners dot com [2011-02-17 05:45:31]
If you want to confront a date stored into mysql as a date field (not a datetime) and a date specified by a literal string, be sure to add "midnight" to the literal string, otherwise they won't match:
<?php
//I.E.: today is 17/02/2011
echo strtotime('2011-01-01'); //1293836400
echo strtotime('first day of last month'); //1293888128 Note: it's different from the previous one, since it computes also the seconds passed from midnight!!! So this one is always greater than simple '2011-01-01'
echo strtotime('midnight first day of last monty');//1293836400 Note: it's the same as '2011-01-01'
?>
[#20] nyctimus at yahoo dot com [2011-01-05 17:54:55]
strtotime() produces different output on 32 and 64 bit systems running PHP 5.3.3 (as mentioned previously). This affects the "zero date" ("0000-00-00 00:00:00") as well as dates outside the traditional 32 date range.
strtotime("0000-00-00 00:00:00") returns FALSE on a 32 bit system.
strtotime("0000-00-00 00:00:00") returns -62169955200 on a 64 bit system.
[#21] x at xero dot nu [2010-12-21 09:41:00]
strtotime is awesome for converting dates.
in this example i will make an RSS date, an
ATOM date, then convert them to a human
readable m/d/Y dates.
<?php
$rss = date("r");
$atom = date("c");
$human1 = date('m/d/Y', strtotime($rss));
$human2 = date('m/d/Y', strtotime($atom));
echo $rss."<br />".$atom."<br />".$human1."<br />".$human2;
?>
[#22] marcodemaio at vanylla dot it [2010-10-27 11:09:50]
NOTE: strtotime returns different values when the Week day does not match the date.
Simple example:
<?php
$d1 = strtotime("26 Oct 0010 12:00:00 +0100");
$d2 = strtotime("Tue, 26 Oct 0010 12:00:00 +0100");
$d3 = strtotime("Sun, 26 Oct 0010 12:00:00 +0100"); //But Oct 26 is a Tuesday, NOT a Sunday.
echo $d1; //ok 1288090800 that is "26 Ott 2010 - 11:00";
echo $d2; //ok 1288090800 that is "26 Ott 2010 - 11:00";
echo $d3; //WRONG! 1288522800 that is "31 Ott 2010 - 11:00";
?>
Sometime I found RSS feeds that contains week days that do not match the date.
A possible solution is to remove useless week day before passing the date string into strtotime, example:
<?php
$date_string = "Sun, 26 Oct 0010 12:00:00 +0100";
if( ($comma_pos = strpos($date_string, ',')) !== FALSE )
$date_string = substr($date_string, $comma_pos + 1);
$d3 = strtotime($date_string);
?>
[#23] sam at frontiermedia dot net dot au [2010-09-28 03:23:21]
I've had a little trouble with this function in the past because (as some people have pointed out) you can't really set a locale for strtotime. If you're American, you see 11/12/10 and think "12 November, 2010". If you're Australian (or European), you think it's 11 December, 2010. If you're a sysadmin who reads in ISO, it looks like 10th December 2011.
The best way to compensate for this is by modifying your joining characters. Forward slash (/) signifies American M/D/Y formatting, a dash (-) signifies European D-M-Y and a period (.) signifies ISO Y.M.D.
Observe:
<?php
echo date("jS F, Y", strtotime("11.12.10"));
// outputs 10th December, 2011
echo date("jS F, Y", strtotime("11/12/10"));
// outputs 12th November, 2010
echo date("jS F, Y", strtotime("11-12-10"));
// outputs 11th December, 2010
?>
Hope this helps someone!
[#24] Tim [2010-09-18 21:52:32]
Unlike "yesterday 14:00", "14:00 yesterday" will return 00:00 of yesterday.
[#25] Stefan Kunstmann [2010-07-30 06:32:21]
UK dates (eg. 27/05/1990) won't work with strotime, even with timezone properly set.
[red., derick]: What you instead should do is:
<?php
$date = date_create_from_format('d/m/y', '27/05/1990');
?>
That does not make it a timestamp, but a DateTime object, which is much more versatile instead.
[#26] Alex Austin [2010-03-16 08:18:54]
Seems in PHP5 there are new values that can be passed to the function:
<?php
echo time();
echo '<br>';
echo strtotime('noon');
echo '<br>';
echo strtotime('midnight');
echo '<br>';
echo strtotime('10am');
echo '<br>';
echo strtotime('2pm');
?>
[#27] katylava at gmail dot com [2010-02-08 11:04:45]
You should play around with strtotime() before you decide what it can't do. for example:
<?php
# on 2/8/2010
date('m/d/y', strtotime('first day')); # 02/01/10
date('m/d/y', strtotime('last day')); # 02/28/10
date('m/d/y', strtotime('last day next month')); # 03/31/10
date('m/d/y', strtotime('last day last month')); # 01/31/10
date('m/d/y', strtotime('2009-12 last day')); # 12/31/09 - this doesn't work if you reverse the order of the year and month
date('m/d/y', strtotime('2009-03 last day')); # 03/31/09
date('m/d/y', strtotime('2009-03')); # 03/01/09
date('m/d/y', strtotime('last day of march 2009')); # 03/31/09
date('m/d/y', strtotime('last day of march')); # 03/31/10
?>
[#28] Leon F. [2009-11-03 18:58:19]
I needed to generate timestamps for specific days of the week in a month (e.g. the 2nd wednesday or the 3rd friday). After messing about with different syntax, I found this works pretty consistently:
<?php
strtotime('+0 week sun nov 2009'); // first sunday in nov 2009
strtotime('+1 week sun nov 2009'); // second sunday
strtotime('-1 week sun nov 2009'); // last sunday in oct 2009
?>
This is helping me considerably in parsing ical spec's RRULE sets for example.
[#29] kooshal at live dot com [2009-10-21 06:59:54]
when using PHP 5.3, you must use date_default_timezone_set() to set the time zone otherwise you will get warning similar to this (if you have display_errors=On)??
Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Dubai' for '4.0/no DST' instead in path/to/php/script.php
on line ##
Example:
date_default_timezone_set('Indian/Mauritius');
For a list of supported timezones in PHP, see http://www.php.net/manual/en/timezones.php
[#30] Anonymous [2009-10-05 15:47:53]
This function DOES NOT work from left-to-right as one would think. This function parses the string as a whole, then applies the intervals by size (year, month, ...). Take the following example:
<?php
$Date = strtotime('2011-02-22'); // February 22nd, 2011. 28 days in this month, 29 next year.
echo date('n/j/Y', strtotime('+1 year, +7 days', $Date)); // add 1 year and 7 days. prints 2/29/2012
echo "<br />";
echo date('n/j/Y', strtotime('+7 days, +1 year', $Date)); // add 7 days and 1 year, but this also prints 2/29/2012
echo "<br />";
echo date('n/j/Y', strtotime('+1 year', strtotime('+7 days', $Date))); // this prints 3/1/2012, what the 2nd would do if it was left-to-right
?>
[#31] chris at teamsiems dot com [2009-09-08 10:20:27]
It took me a while to notice that strtotime starts searching from just after midnight of the first day of the month. So, if the month starts on the day you search for, the first day of the search is actually the next occurrence of the day.
In my case, when I look for first Tuesday of the current month, I need to include a check to see if the month starts on a Tuesday.
<?php
if (date("l", strtotime("$thisMonth $thisYear"))=='Tuesday') {
echo "<p>This month starts on a Tuesday. Use \"$thisMonth $thisYear\" to check for first Tuesday.</p>\n";
} else {
echo "<p>This month does not start on a Tuesday. Use \"first tuesday $thisMonth $thisYear\" to check for first Tuesday.</p>\n";
}
?>
[#32] michal dot kocarek at brainbox dot cz [2009-08-07 06:35:57]
strtotime() also returns time by year and weeknumber. (I use PHP 5.2.8, PHP 4 does not support it.) Queries can be in two forms:
- "yyyyWww", where yyyy is 4-digit year, W is literal and ww is 2-digit weeknumber. Returns timestamp for first day of week (for me Monday)
- "yyyy-Www-d", where yyyy is 4-digit year, W is literal, ww is 2-digit weeknumber and dd is day of week (1 for Monday, 7 for Sunday)
<?php
// Get timestamp of 32nd week in 2009.
strtotime('2009W32'); // returns timestamp for Mon, 03 Aug 2009 00:00:00
// Weeknumbers < 10 must be padded with zero:
strtotime('2009W01'); // returns timestamp for Mon, 29 Dec 2008 00:00:00
// strtotime('2009W1'); // error! returns false
// See timestamp for Tuesday in 5th week of 2008
strtotime('2008-W05-2'); // returns timestamp for Tue, 29 Jan 2008 00:00:00
?>
Weeknumbers are (probably) computed according to ISO-8601 specification, so doing date('W') on given timestamps should return passed weeknumber.
[#33] Travis Pulley [2009-08-06 00:11:35]
Be aware that if you are running 5.2.8, there is a memory leak with this function and it could cost someone valuable time finding out what the problem was. Per usual, running the latest (minor) version tends to be a good idea.
See here: http://bugs.php.net/bug.php?id=46889
[#34] php at davidstockton dot com [2009-07-30 21:04:46]
Adding a note to an already long page:
Try to be as specific as you can with the string you pass in. For example
<?php
echo date('F', strtotime('February'));
?>
is not specific enough. Depending on the day of the month, you may get a different response. For a non-leap year, you'll get March if the _current day of the month_ is the 29th, 30th or 31st. If it's a leap year, you'll get March on the 30th or 31st of the month. The same thing will happen on the 31st of any month when you pass in the name of any month with less than 31 days. This happens because the strtotime() function will fill in missing parts from the current day.
Assuming today is July 31, the timestamp returned by strtotime('February') will ultimately be seen as February 31 (non-existant obviously), which then is interpreted as March 3, thus giving a month name of March.
Interestingly, adding the year or the day will give you back the expected month.
[#35] Killermonk [2009-06-25 16:13:48]
You are not restricted to the same date ranges when running PHP on a 64-bit machine. This is because you are using 64-bit integers instead of 32-bit integers (at least if your OS is smart enough to use 64-bit integers in a 64-bit OS)
The following code will produce difference output in 32 and 64 bit environments.
var_dump(strtotime('1000-01-30'));
32-bit PHP: bool(false)
64-bit PHP: int(-30607689600)
This is true for php 5.2.* and 5.3
Also, note that the anything about the year 10000 is not supported. It appears to use only the last digit in the year field. As such, the year 10000 is interpretted as the year 2000; 10086 as 2006, 13867 as 2007, etc
[#36] viper7 at viper-7 dot com [2009-05-28 03:42:51]
Observed date formats that strtotime expects, it can be quite confusing, so hopefully this makes things a little clearer for some.
mm/dd/yyyy - 02/01/2003 - strtotime() returns : 1st February 2003
mm/dd/yy - 02/01/03 - strtotime() returns : 1st February 2003
yyyy/mm/dd - 2003/02/01 - strtotime() returns : 1st February 2003
dd-mm-yyyy - 01-02-2003 - strtotime() returns : 1st February 2003
yy-mm-dd - 03-02-01 - strtotime() returns : 1st February 2003
yyyy-mm-dd - 2003-02-01 - strtotime() returns : 1st February 2003
[#37] Peter [2009-03-30 05:57:16]
If you look for function to convert date from RSS pubDate, always make sure to check correct input format, found that for date("r") or "D, d M o G:i:s T" strtotime may return wrong result.
[#38] fuhrysteve at gmail dot com [2009-03-21 07:18:49]
Here's a hack to make this work for MS SQL's datetime junk, since strtotime() has issues with fractional seconds.
<?php
$MSSQLdatetime = "Feb 7 2009 09:48:06:697PM";
$newDatetime = preg_replace('/:[0-9][0-9][0-9]/','',$MSSQLdatetime);
$time = strtotime($newDatetime);
echo $time."\n";
?>
[#39] CEO at CarPool2Camp dot org [2009-03-12 05:41:51]
Another way to get the last day of a given month is to use date('t');
[#40] jaymin at gmx dot net [2008-08-08 06:48:13]
strtotime() seems to treat dates delimited by slashes as m/d/y and dates delimited by dashes are treated as d-m-y.
<?php
print date('Y-m-d', strtotime("06/08/2008"));
?>
returns 2008-06-08
while
<?php
print date('Y-m-d', strtotime("06-08-2008"));
?>
returns 2008-08-06
Using PHP 5.2.6
[#41] andrew dot myers at civicwifi dot com [2007-12-05 10:42:50]
Here is a list of differences between PHP 4 and PHP 5 that I have found
(specifically PHP 4.4.2 and PHP 5.2.3).
<?php
$ts_from_nothing = strtotime();
var_dump($ts_from_nothing);
// PHP 5
// bool(false)
// WARNING: Wrong parameter count...
// PHP 4
// NULL
// WARNING: Wrong parameter count...
// remember that unassigned variables evaluate to NULL
$ts_from_null = strtotime($null);
var_dump($ts_from_null)...
// PHP 5
// bool(false)
// throws a NOTICE: Undefined variable
// PHP 4
// current time
// NOTICE: Undefined variable $null...
// NOTICE: Called with empty time parameter...
$ts_from_empty = strtotime("");
var_dump($ts_from_empty);
// PHP 5
// bool(false)
// PHP 4
// current time
// NOTICE: Called with empty time parameter
$ts_from_bogus = strtotime("not a date");
var_dump($ts_from_bogus);
// PHP 5
// bool(false)
// PHP 4
// -1
?>
[#42] sgutauckis [2006-10-05 12:02:26]
The following might produce something different than you might expect:
<?php
echo date('l, F jS Y', strtotime("third wednesday", strtotime("2006-11-01"))) . "<br>";
echo date('l, F jS Y', strtotime("third sunday", strtotime("2006-01-01")));
?>
Produces:
Wednesday, November 22nd 2006
Sunday, January 22nd 2006
The problem stems from strtotime when the requested day falls on the date passed to strtotime. If you look at your calendar you will see that they should return:
Wednesday, November 15th 2006
Sunday, January 15th 2006
Because the date falls on the day requested it skips that day.
[#43] kyle at frozenonline dot com [2004-01-01 15:24:21]
I was having trouble parsing Apache log files that consisted of a time entry (denoted by %t for Apache configuration). An example Apache-date looks like: [21/Dec/2003:00:52:39 -0500]
Apache claims this to be a 'standard english format' time. strtotime() feels otherwise.
I came up with this function to assist in parsing this peculiar format.
<?php
function from_apachedate($date)
{
list($d, $M, $y, $h, $m, $s, $z) = sscanf($date, "[%2d/%3s/%4d:%2d:%2d:%2d %5s]");
return strtotime("$d $M $y $h:$m:$s $z");
}
?>
Hope it helps anyone else seeking such a conversion.