1. 目录清单
使用下面的 PHP 代码片段可以在一个目录中列出所有文件和文件夹。
'."\n"(
语法:
2. 检测用户语言
使用下面的 PHP 代码片段可以检测用户浏览器所使用的语言。
3. 查看 CSV 文件
语法:
4. 从 PHP 数据创建 CSV 文件
语法:
5. 解析 XML 数据
A
K
<span style="color: #800080">$xml = <span style="color: #008080">simplexml_load_string(<span style="color: #800080">$xml_string<span style="color: #000000">);
<span style="color: #008000">//<span style="color: #008000">loop through the each node of molecule
<span style="color: #0000ff">foreach (<span style="color: #800080">$xml->molecule <span style="color: #0000ff">as <span style="color: #800080">$record<span style="color: #000000">)
{
<span style="color: #008000">//<span style="color: #008000">attribute are accessted by
<span style="color: #0000ff">echo <span style="color: #800080">$record['name'],' '<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">node are accessted by -> operator
<span style="color: #0000ff">echo <span style="color: #800080">$record->symbol,' '<span style="color: #000000">;
<span style="color: #0000ff">echo <span style="color: #800080">$record->code,'
'<span style="color: #000000">;
}
6. 解析 JSON 数据
7. 获取当前页面 URL
这个 PHP 片段可以帮助你让用户登录后直接跳转到之前浏览的页面。
语法:
8. 从任意的 Twitter 账号获取最新的 Tweet
语法:
9. 转发数量
使用这个 PHP 片段可以检测你的页面 URL 有多少转发数量。
语法:
10. 计算两个日期的差
- Calculate differences between two dates with precise semantics. Based on PHPs DateTime::diff()
- implementation by Derick Rethans. Ported to PHP by Emil H,2011-05-02. No rights reserved.
- See here for original code:
- http://svn.php.net/viewvc/php/php-src/trunk/ext/date/lib/tm2unixtime.c?revision=302890&view=markup
- http://svn.php.net/viewvc/php/php-src/trunk/ext/date/lib/interval.c?revision=298973&view=markup
<span style="color: #0000ff">function _date_range_limit(<span style="color: #800080">$start,<span style="color: #800080">$end,<span style="color: #800080">$adj,<span style="color: #800080">$a,<span style="color: #800080">$b,<span style="color: #800080">$result<span style="color: #000000">)
{
<span style="color: #0000ff">if (<span style="color: #800080">$result[<span style="color: #800080">$a] < <span style="color: #800080">$start<span style="color: #000000">) {
<span style="color: #800080">$result[<span style="color: #800080">$b] -= <span style="color: #008080">intval((<span style="color: #800080">$start - <span style="color: #800080">$result[<span style="color: #800080">$a] - 1) / <span style="color: #800080">$adj) + 1<span style="color: #000000">;
<span style="color: #800080">$result[<span style="color: #800080">$a] += <span style="color: #800080">$adj <span style="color: #008080">intval((<span style="color: #800080">$start - <span style="color: #800080">$result[<span style="color: #800080">$a] - 1) / <span style="color: #800080">$adj + 1<span style="color: #000000">);
}
<span style="color: #0000ff">if (<span style="color: #800080">$result[<span style="color: #800080">$a] >= <span style="color: #800080">$end<span style="color: #000000">) {
<span style="color: #800080">$result[<span style="color: #800080">$b] += <span style="color: #008080">intval(<span style="color: #800080">$result[<span style="color: #800080">$a] / <span style="color: #800080">$adj<span style="color: #000000">);
<span style="color: #800080">$result[<span style="color: #800080">$a] -= <span style="color: #800080">$adj <span style="color: #008080">intval(<span style="color: #800080">$result[<span style="color: #800080">$a] / <span style="color: #800080">$adj<span style="color: #000000">);
}
<span style="color: #0000ff">return <span style="color: #800080">$result<span style="color: #000000">;
}
<span style="color: #0000ff">function _date_range_limit_days(<span style="color: #800080">$base,<span style="color: #800080">$result<span style="color: #000000">)
{
<span style="color: #800080">$days_in_month_leap = <span style="color: #0000ff">array(31,31,29,30,31<span style="color: #000000">);
<span style="color: #800080">$days_in_month = <span style="color: #0000ff">array(31,28,31<span style="color: #000000">);
_date_range_limit(1,13,12,"m","y",&<span style="color: #800080">$base<span style="color: #000000">);
<span style="color: #800080">$year = <span style="color: #800080">$base["y"<span style="color: #000000">];
<span style="color: #800080">$month = <span style="color: #800080">$base["m"<span style="color: #000000">];
<span style="color: #0000ff">if (!<span style="color: #800080">$result["invert"<span style="color: #000000">]) {
<span style="color: #0000ff">while (<span style="color: #800080">$result["d"] < 0<span style="color: #000000">) {
<span style="color: #800080">$month--<span style="color: #000000">;
<span style="color: #0000ff">if (<span style="color: #800080">$month < 1<span style="color: #000000">) {
<span style="color: #800080">$month += 12<span style="color: #000000">;
<span style="color: #800080">$year--<span style="color: #000000">;
}
<span style="color: #800080">$leapyear = <span style="color: #800080">$year % 400 == 0 || (<span style="color: #800080">$year % 100 != 0 && <span style="color: #800080">$year % 4 == 0<span style="color: #000000">);
<span style="color: #800080">$days = <span style="color: #800080">$leapyear ? <span style="color: #800080">$days_in_month_leap[<span style="color: #800080">$month] : <span style="color: #800080">$days_in_month[<span style="color: #800080">$month<span style="color: #000000">];
<span style="color: #800080">$result["d"] += <span style="color: #800080">$days<span style="color: #000000">;
<span style="color: #800080">$result["m"]--<span style="color: #000000">;
}
} <span style="color: #0000ff">else<span style="color: #000000"> {
<span style="color: #0000ff">while (<span style="color: #800080">$result["d"] < 0<span style="color: #000000">) {
<span style="color: #800080">$leapyear = <span style="color: #800080">$year % 400 == 0 || (<span style="color: #800080">$year % 100 != 0 && <span style="color: #800080">$year % 4 == 0<span style="color: #000000">);
<span style="color: #800080">$days = <span style="color: #800080">$leapyear ? <span style="color: #800080">$days_in_month_leap[<span style="color: #800080">$month] : <span style="color: #800080">$days_in_month[<span style="color: #800080">$month<span style="color: #000000">];
<span style="color: #800080">$result["d"] += <span style="color: #800080">$days<span style="color: #000000">;
<span style="color: #800080">$result["m"]--<span style="color: #000000">;
<span style="color: #800080">$month++<span style="color: #000000">;
<span style="color: #0000ff">if (<span style="color: #800080">$month > 12<span style="color: #000000">) {
<span style="color: #800080">$month -= 12<span style="color: #000000">;
<span style="color: #800080">$year++<span style="color: #000000">;
}
}
}
<span style="color: #0000ff">return <span style="color: #800080">$result<span style="color: #000000">;
}
<span style="color: #0000ff">function _date_normalize(<span style="color: #800080">$base,<span style="color: #800080">$result<span style="color: #000000">)
{
<span style="color: #800080">$result = _date_range_limit(0,60,"s","i",<span style="color: #800080">$result<span style="color: #000000">);
<span style="color: #800080">$result = _date_range_limit(0,"h",24,"d",<span style="color: #800080">$result<span style="color: #000000">);
<span style="color: #800080">$result = _date_range_limit_days(&<span style="color: #800080">$base,&<span style="color: #800080">$result<span style="color: #000000">);
<span style="color: #800080">$result = _date_range_limit(0,<span style="color: #800080">$result<span style="color: #000000">);
<span style="color: #0000ff">return <span style="color: #800080">$result<span style="color: #000000">;
}
<span style="color: #008000">/<span style="color: #008000">*
<span style="color: #008000">*/
<span style="color: #0000ff">function _date_diff(<span style="color: #800080">$one,<span style="color: #800080">$two<span style="color: #000000">)
{
<span style="color: #800080">$invert = <span style="color: #0000ff">false<span style="color: #000000">;
<span style="color: #0000ff">if (<span style="color: #800080">$one > <span style="color: #800080">$two<span style="color: #000000">) {
<span style="color: #0000ff">list(<span style="color: #800080">$one,<span style="color: #800080">$two) = <span style="color: #0000ff">array(<span style="color: #800080">$two,<span style="color: #800080">$one<span style="color: #000000">);
<span style="color: #800080">$invert = <span style="color: #0000ff">true<span style="color: #000000">;
}
<span style="color: #800080">$key = <span style="color: #0000ff">array("y","s"<span style="color: #000000">);
<span style="color: #800080">$a = <span style="color: #008080">array_combine(<span style="color: #800080">$key,<span style="color: #008080">array_map("intval",<span style="color: #008080">explode(" ",<span style="color: #008080">date("Y m d H i s",<span style="color: #800080">$one<span style="color: #000000">))));
<span style="color: #800080">$b = <span style="color: #008080">array_combine(<span style="color: #800080">$key,<span style="color: #800080">$two<span style="color: #000000">))));
<span style="color: #800080">$result = <span style="color: #0000ff">array<span style="color: #000000">();
<span style="color: #800080">$result["y"] = <span style="color: #800080">$b["y"] - <span style="color: #800080">$a["y"<span style="color: #000000">];
<span style="color: #800080">$result["m"] = <span style="color: #800080">$b["m"] - <span style="color: #800080">$a["m"<span style="color: #000000">];
<span style="color: #800080">$result["d"] = <span style="color: #800080">$b["d"] - <span style="color: #800080">$a["d"<span style="color: #000000">];
<span style="color: #800080">$result["h"] = <span style="color: #800080">$b["h"] - <span style="color: #800080">$a["h"<span style="color: #000000">];
<span style="color: #800080">$result["i"] = <span style="color: #800080">$b["i"] - <span style="color: #800080">$a["i"<span style="color: #000000">];
<span style="color: #800080">$result["s"] = <span style="color: #800080">$b["s"] - <span style="color: #800080">$a["s"<span style="color: #000000">];
<span style="color: #800080">$result["invert"] = <span style="color: #800080">$invert ? 1 : 0<span style="color: #000000">;
<span style="color: #800080">$result["days"] = <span style="color: #008080">intval(<span style="color: #008080">abs((<span style="color: #800080">$one - <span style="color: #800080">$two)/86400<span style="color: #000000">));
<span style="color: #0000ff">if (<span style="color: #800080">$invert<span style="color: #000000">) {
_date_normalize(&<span style="color: #800080">$a,&<span style="color: #800080">$result<span style="color: #000000">);
} <span style="color: #0000ff">else<span style="color: #000000"> {
_date_normalize(&<span style="color: #800080">$b,&<span style="color: #800080">$result<span style="color: #000000">);
}
<span style="color: #0000ff">return <span style="color: #800080">$result<span style="color: #000000">;
}
<span style="color: #800080">$date = "2014-12-04 19:37:22"<span style="color: #000000">;
<span style="color: #0000ff">echo '
'<span style="color: #000000">;'<span style="color: #000000">;
<span style="color: #008080">print_r( _date_diff( <span style="color: #008080">strtotime(<span style="color: #800080">$date),<span style="color: #008080">time<span style="color: #000000">() ) );
<span style="color: #0000ff">echo '
?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。