<?php
functionDateAdd($date$int$unit="d"){//时间的增加(还可以改进成时分秒都可以增加有时间再补上)
$dateArr=explode(""$date);
$value[$unit]=$int; //OSPHPCOmCN
returndate("Ymd"mktime($dateArr[]+$value[m]$dateArr[]+$value[d]$dateArr[]+$value[y])); //OSPHPCOmCN
}
functionGetWeekDay($date){//计算出给出的日期是星期几
$dateArr=explode(""$date);
returndate("w"mktime($dateArr[]$dateArr[]$dateArr[])); }
?>
<?
functionGetIP(){//获取IP
if($_SERVER["HTTP_X_FORWARDED_FOR"])
$ip=$_SERVER["HTTP_X_FORWARDED_FOR"];
elseif($_SERVER["HTTP_CLIENT_IP"])
$ip=$_SERVER["HTTP_CLIENT_IP"];
elseif($_SERVER["REMOTE_ADDR"])
$ip=$_SERVER["REMOTE_ADDR"]; getenv("HTTP_X_FORWARDED_FOR"))
$ip=getenv("HTTP_X_FORWARDED_FOR");
elseif(getenv("HTTP_CLIENT_IP"))
$ip=getenv("HTTP_CLIENT_IP");
elseif(getenv("REMOTE_ADDR"))
$ip=getenv("REMOTE_ADDR");
else
$ip="Unknown";
return$ip;
}
?> <?
functioncheck_date($date){//检查日期是否合法日期
$dateArr=explode(""$date);
if(is_numeric($dateArr[])&&is_numeric($dateArr[])&&is_numeric($dateArr[])){ checkdate($dateArr[]$dateArr[]$dateArr[]);
}
returnfalse;
}
functioncheck_time($time){//检查时间是否合法时间 //开源OSPhPCOMCN
$timeArr=explode(":"$time);
if(is_numeric($timeArr[])&&is_numeric($timeArr[])&&is_numeric($timeArr[])){ $timeArr[]>=&&$timeArr[]<=)&&($timeArr[]>=&&$timeArr[]<=)&&($timeArr[]>=&&$timeArr[]<=)) //开源OSPhPCOMCN
returntrue;
else
returnfalse;
}
returnfalse;
}
functionDateDiff($date$date$unit=""){//时间比较函数返回两个日期相差几秒几分钟几小时或几天 //开源代码OSPhPCOmCN
switch($unit){
cases:
$dividend=;
break;
casei:
$dividend=;
break;
caseh:
$dividend=;
break;
cased:
$dividend=;
break; //oSPHPCOMCN
default:
$dividend=;
}
$time=strtotime($date);
$time=strtotime($date);
if($time&&$time) $time$time)/$dividend;
returnfalse;
}
?>
PHP重定向
<?
方法一header("Location:indexphp");
方法二echo"<scrīpt>windowlocation="$PHP_SELF";</scrīpt>"; //PHP开源代码
方法三echo"<METAHTTPEQUIV="Refresh"CONTENT=";URL=indexphp">";
?>
获取访问者浏览器
<?
functionbrowse_infor()
{
$browser="";$browserver=""; $Browsers=array("Lynx""MOSAIC""AOL""Opera""JAVA""MacWeb""WebExplorer""OmniWeb");
$Agent=$GLOBALS["HTTP_USER_AGENT"];
for($i=;$i<=;$i++) //OsPHPCOMCN
{
if(strpos($Agent$Browsers[$i]))
{
$browser=$Browsers[$i];
$browserver="";
}
}
if(ereg("Mozilla"$Agent)&&!ereg("MSIE"$Agent))
{
$temp=explode("("$Agent);$Part=$temp[];
$temp=explode("/"$Part);$browserver=$temp[]; $temp=explode(""$browserver);$browserver=$temp[];
$browserver=preg_replace("/([d]+)/"""$browserver);
$browserver="$browserver"; //OSPHPCOMCn开源
$browser="NetscapeNavigator";
}
if(ereg("Mozilla"$Agent)&&ereg("Opera"$Agent))
{
$temp=explode("("$Agent);$Part=$temp[]; $temp=explode(")"$Part);$browserver=$temp[];
$temp=explode(""$browserver);$browserver=$temp[];
$browserver=preg_replace("/([d]+)/"""$browserver);
$browserver="$browserver";
$browser="Opera";
}
if(ereg("Mozilla"$Agent)&&ereg("MSIE"$Agent)) //OSPHPCOmCN
{
$temp=explode("("$Agent);$Part=$temp[];
$temp=explode(";"$Part);$Part=$temp[]; $temp=explode(""$Part);$browserver=$temp[];
$browserver=preg_replace("/([d]+)/"""$browserver);
$browserver="$browserver"; $browser="InternetExplorer";
}
if($browser!="")
{
$browseinfo="$browser$browserver";
}
else
{
$browseinfo="Unknown";
}
return$browseinfo;
}
//调用方法$browser=browseinfo();直接返回结果
?> //oSPHPCOM获取访问者操作系统
<?
functionosinfo(){
$os="";
$Agent=$GLOBALS["HTTP_USER_AGENT"];
if(eregi(win$Agent)&&strpos($Agent)){ //PHP开源代码
$os="Windows";
}
elseif(eregi(winx$Agent)&&strpos($Agent)){
$os="WindowsME";
}
elseif(eregi(win$Agent)&&ereg($Agent)){ //OSPHPCOmCN
$os="Windows";
}
elseif(eregi(win$Agent)&&eregi(nt$Agent)){
$os="Windows";
}
elseif(eregi(win$Agent)&&eregi(nt$Agent)){ //开源代码OSPHPCOMCn
$os="WindowsNT";
}
elseif(eregi(win$Agent)&&eregi(nt$Agent)){
$os="WindowsXP";
}
elseif(eregi(win$Agent)&&ereg($Agent)){ $os="Windows";
}
elseif(eregi(linux$Agent)){
$os="Linux";
}
elseif(eregi(unix$Agent)){
$os="Unix"; eregi(sun$Agent)&&eregi(os$Agent)){
$os="SunOS";
}
elseif(eregi(ibm$Agent)&&eregi(os$Agent)){ $os="IBMOS/";
}
elseif(eregi(Mac$Agent)&&eregi(PC$Agent)){
$os="Macintosh";
}
elseif(eregi(PowerPC$Agent)){
$os="PowerPC";
}
elseif(eregi(AIX$Agent)){
$os="AIX";
}
elseif(eregi(HPUX$Agent)){
$os="HPUX"; //PHP开源代码
}
elseif(eregi(NetBSD$Agent)){
$os="NetBSD";
}
elseif(eregi(BSD$Agent)){
$os="BSD";
}
elseif(ereg(OSF$Agent)){
$os="OSF";
}
elseif(ereg(IRIX$Agent)){
$os="IRIX";
}
elseif(eregi(FreeBSD$Agent)){
$os="FreeBSD"; $os==)$os="Unknown";
return$os;
}
//调用方法$os=os_infor();
?>
文件格式类
<?
$mime_types=array(
gif=>image/gif
jpg=>image/jpeg
jpeg=>image/jpeg
jpe=>image/jpeg
bmp=>image/bmp
png=>image/png
tif=>image/tiff
tiff=>image/tiff
pict=>image/xpict
pic=>image/xpict
pct=>image/xpict
tif=>image/tiff
tiff=>image/tiff psd=>image/xphotoshop
swf=>application/xshockwaveflash
js=>application/xjavascrīpt
pdf=>application/pdf
ps=>application/postscrīpt
eps=>application/postscrīpt ai=>application/postscrīpt
wmf=>application/xmsmetafile
css=>text/css
htm=>text/html
html=>text/html
txt=>text/plain xml=>text/xml
wml=>text/wml
wbmp=>image/vndwapwbmp
mid=>audio/midi
wav=>audio/wav
mp=>audio/mpeg mp=>audio/mpeg
avi=>video/xmsvideo
mpeg=>video/mpeg
mpg=>video/mpeg
qt=>video/quicktime
mov=>video/quicktime //开源代码OSPHPCOMCn
lha=>application/xlha
lzh=>application/xlha
z=>application/xcompress
gtar=>application/xgtar
gz=>application/xgzip
gzip=>application/xgzip tgz=>application/xgzip
tar=>application/xtar
bz=>application/bzip
zip=>application/zip
arj=>application/xarj
rar=>application/xrarcompressed
hqx=>application/macbinhex
sit=>application/xstuffit
bin=>application/xmacbinary
uu=>text/xuuencode
uue=>text/xuuencode
latex=>application/xlatex //PHP开源代码
ltx=>application/xlatex
tcl=>application/xtcl
pgp=>application/pgp
asc=>application/pgp
exe=>application/xmsdownload
doc=>application/msword //OSPHPcomCN
rtf=>application/rtf
xls=>application/vndmsexcel
ppt=>application/vndmspowerpoint
mdb=>application/xmsaccess
wri=>application/xmswrite
);
?>
elseif(
return
if((
return(float)(
}
elseif(
}
if(
生成excel文档
<?
header("Content
type:application/vnd
ms
excel");
header("Content
Disposition:filename=test
xls");
echo"test
t";
echo"test
tn";
echo"test
t";
echo"test
tn";
echo"test
t"; "test
tn";
echo"test
t";
echo"test
tn";
echo"test
t";
echo"test
tn";
echo"test
t";
echo"test
tn";
//改动相应文件头就可以输出
doc
xls等文件格式了
?>
时间比较问题
举一个简单例子说明:比如一个论坛对当天发表的贴子用new图片标记一下
方法一
<?
//$db
>rows[$i][date]中为数据库中datetime字段值
$today=time();
$theDay=date("Y
m
dH:i:s"
$today
*
);
$newTag=$db
>rows[$i][date]>=$theDay?"<imgsrc=
/image/newinfor
gif
>":"";
//方法二
$newTag=$db
>rows[$i][date]>=date("Y
m
d
:
:
")?"<imgsrc=
/image/newinfor
gif
>":"";
?>
//提取页面和浏览器提交的变量
作用相当于使PHP
INI开了全局变量
<?
@extract($_SERVER
EXTR_SKIP);
@extract($_SESSION
EXTR_SKIP);
@extract($_POST
EXTR_SKIP);
@extract($_FILES
EXTR_SKIP); //OSPHP
COm
CN
@extract($_GET
EXTR_SKIP);
@extract($_ENV
EXTR_SKIP);
?>
//写入文件函数
<?
functionwritetofile($file_name
$data
$method="w"){
$filenum=fopen($file_name
$method);
flock($filenum
LOCK_EX);
$file_data=fwrite($filenum
$data);
fclose($filenum);
return$file_data;
}
?>
//页面快速转向
<?
functionturntopage($url="index
php"
$info="页面转向中
"
$second=
){
print"<html>n<head>n<title>页面转向中
</title>n";
print"<metahttp
equiv="refresh"content="$second;url=$url">n";
print"<styletype="text/css">n<!
n";
print"td{font
family:"Verdana"
"Arial";font
size:
px}n";
print"A{COLOR:#
;TEXT
DECORATION:none}n";
print"
>n</style>n";
print"</head>n<body>n";
print"<tablewidth="
%"border="
"align="center">n"; //PHP开源代码
print"<tr>n";
print"<tdheight="
"></td>n";
print"</tr>n";
print"<tr>n";
print"<tdalign="center">n";
print"<tablewidth="
%"border="
"cellpadding="
"bgcolor="#AA
FFF">n";
//PHP开源代码
print"<tr>n";
print"<tdheight="
"align="center">页面转向提示信息</td>n";
print"</tr>n";
print"<tr>n";
print"<tdalign="center">$info</td>n";
print"</tr>n";
print"<tr>n"; //开源OSPhP
COM
CN
print"<tdalign="center">n";
print"<ahref="$url">如果你的浏览器不支持自动跳转
请按这里</a></td>n";
print"</tr>n";
print"</tr>n";
print"</table></td>n";
print"</tr>n";
print"<tr>n";
print"<tdheight="
"></td>n";
print"</tr>n";
print"</table>n";
print"</body>n</html>";
exit;
?>
echo