php svn操作类

 更新时间:2016年11月25日 17:31  
以前我们开发大型项目时都会用到svn来同步,因为开发产品的人过多,所以我们会利用软件来管理,今天发有一居然可以利用php来管理svn哦,好了看看吧。

代码如下
以前我们开发大型项目时都会用到svn来同步,因为开发产品的人过多,所以我们会利用软件来管理,今天发有一居然可以利用php来管理svn哦,好了看看吧。
 代码如下 复制代码

<?php
/**
*
* This class for execute the external program of svn
*
* @auth Seven Yang http://www.111cn.net
*
*/
class SvnPeer
{
/**
* List directory entries in the repository
*
* @param string a specific project repository path
* @return bool true, if validated successfully, otherwise false
*/
static public function ls($repository)
{
$command = "svn ls " . $repository;
$output = SvnPeer::runCmd($command);
$output = implode("<br>", $output);
if (strpos($output, 'non-existent in that revision')) {
return false;
}
return "<br>" . $command . "<br>" . $output;
}
/**
* Duplicate something in working copy or repository, remembering history
*
* @param $src
* @param $dst
* @param $comment string specify log message
* @return bool true, if copy successfully, otherwise return the error message
*
* @todo comment need addslashes for svn commit
*/
static public function copy($src, $dst, $comment)
{
$command = "svn cp $src $dst -m '$comment'";
$output = SvnPeer::runCmd($command);
$output = implode("<br>", $output);
if (strpos($output, 'Committed revision')) {
return true;
}
return "<br>" . $command . "<br>" . $output;
}
/**
* Remove files and directories from version control
*
* @param $url
* @return bool true, if delete successfully, otherwise return the error message
*
* @todo comment need addslashes for svn commit
*/
static public function delete($url, $comment)
{
$command = "svn del $url -m '$comment'";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);
if (strpos($output, 'Committed revision')) {
return true;
}
return "<br>" . $command . "<br>" . $output;
}
/**
* Move and/or rename something in working copy or repository
*
* @param $src string trunk path
* @param $dst string new branch path
* @param $comment string specify log message
* @return bool true, if move successfully, otherwise return the error message
*
* @todo comment need addslashes for svn commit
*/
static public function move($src, $dst, $comment)
{
$command = "svn mv $src $dst -m '$comment'";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);
if (strpos($output, 'Committed revision')) {
return true;
}
return "<br>" . $command . "<br>" . $output;
}
/**
* Create a new directory under version control
*
* @param $url string
* @param $comment string the svn message
* @return bool true, if create successfully, otherwise return the error message
*
* @todo comment need addslashes for svn commit
*/
static public function mkdir($url, $comment)
{
$command = "svn mkdir $url -m '$comment'";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);
if (strpos($output, 'Committed revision')) {
return true;
}
return "<br>" . $command . "<br>" . $output;
}
static public function diff($pathA, $pathB)
{
$output = SvnPeer::runCmd("svn diff $pathA $pathB");
return implode('<br>', $output);
}
static public function checkout($url, $dir)
{
$command = "cd $dir && svn co $url";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);
if (strstr($output, 'Checked out revision')) {
return true;
}
return "<br>" . $command . "<br>" . $output;
}
static public function update($path)
{
$command = "cd $path && svn up";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);
preg_match_all("/[0-9]+/", $output, $ret);
if (!$ret[0][0]){
return "<br>" . $command . "<br>" . $output;
}
return $ret[0][0];
}
static public function merge($revision, $url, $dir)
{
$command = "cd $dir && svn merge -r1:$revision $url";
$output = implode('<br>', SvnPeer::runCmd($command));
if (strstr($output, 'Text conflicts')) {
return 'Command: ' . $command .'<br>'. $output;
}
return true;
}
static public function commit($dir, $comment)
{
$command = "cd $dir && svn commit -m'$comment'";
$output = implode('<br>', SvnPeer::runCmd($command));
if (strpos($output, 'Committed revision') || empty($output)) {
return true;
}
return $output;
}
static public function getStatus($dir)
{
$command = "cd $dir && svn st";
return SvnPeer::runCmd($command);
}
static public function hasConflict($dir)
{
$output = SvnPeer::getStatus($dir);
foreach ($output as $line){
if ('C' == substr(trim($line), 0, 1) || ('!' == substr(trim($line), 0, 1))){
return true;
}
}
return false;
}
/**
* Show the log messages for a set of path with XML
*
* @param path string
* @return log message string
*/
static public function getLog($path)
{
$command = "svn log $path --xml";
$output = SvnPeer::runCmd($command);
return implode('', $output);
}
static public function getPathRevision($path)
{
$command = "svn info $path --xml";
$output = SvnPeer::runCmd($command);
$string = implode('', $output);
$xml = new SimpleXMLElement($string);
foreach ($xml->entry[0]->attributes() as $key=>$value){
if ('revision' == $key) {
return $value;
}
}
}
static public function getHeadRevision($path)
{
$command = "cd $path && svn up";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);
preg_match_all("/[0-9]+/", $output, $ret);
if (!$ret[0][0]){
return "<br>" . $command . "<br>" . $output;
}
return $ret[0][0];
}
/**
* Run a cmd and return result
*
* @param string command line
* @param boolen true need add the svn authentication
* @return array the contents of the output that svn execute
*/
static protected function runCmd($command)
{
$authCommand = ' --username ' . SVN_USERNAME . ' --password ' . SVN_PASSWORD . ' --no-auth-cache --non-interactive --config-dir '.SVN_CONFIG_DIR.'.subversion';
exec($command . $authCommand . " 2>&1", $output);
return $output;
}
}
这是一个最基础的留言板程序了,但是己经有了留言板程序基本功能,很适合于php初学者用用,学习用啊,当然也可以用于企业网站也是很不错的哦。

 

 代码如下 复制代码

<?php
session_start();
$con=mysql_connect('localhost','root','root') or die('链接数据库失败!');
mysql_query('set names utf8');
mysql_select_db('GuestBook');

$pagesize = 10;//每一页显示多少留言记录
if(isset($_GET['page'])&&$_GET['page']!='') $page=$_GET['page'];
else $page=0;

$sql = "SELECT a . * , b.name, b.email, b.qq, c.revert_time, c.revert
  FROM post a
  LEFT JOIN revert c ON ( a.id = c.post_id ) , guest b
  WHERE a.guest_id = b.id
  ORDER BY a.id DESC";
$numRecord = mysql_num_rows(mysql_query($sql));
$totalpage = ceil($numRecord/$pagesize);

$recordSql = $sql. " LIMIT ".$page*$pagesize.",".$pagesize;
$result = mysql_query($recordSql);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHPiask简易留言板</title>
<style type="text/css">
<!--
body {
 margin-left: 0px;
 margin-top: 0px;
}
a:link {
 text-decoration: none;
 color: #FF6600;
}
a:visited {
 text-decoration: none;
}
a:hover {
 text-decoration: underline;
}
a:active {
 text-decoration: none;
}
.STYLE1 {
 color: #FFFFFF;
 font-weight: bold;
 font-size: 16px;
}
td{
font-size:12px;
}
.tdhx {
 font-style: italic;
 line-height: 1.5;
 text-decoration: underline;
}
-->
</style>
<script language="javascript">
function checkInput(){
 var Email = document.getElementById('email');
 var QQ = document.getElementById('qq');
 var name = document.getElementById('name');
 var post = document.getElementById('post');
 //验证用户名:不能超过10个字符(5个汉字),不能输入非法字符,不能为空
 nameValue = name.value.replace(/s+/g,"");
 var SPECIAL_STR = "~!%^&*();"?><[]{}\|,:/=+—";
 var nameflag=true;
 for(i=0;i<nameValue.lenght;i++){
  if (SPECIAL_STR.indexOf(nameValue.charAt(i)) !=-1)
  nameflag=false;
 }
 if(nameValue==''){
  alert('请填写用户名称!'); 
  return false;
 }
 if(nameValue.length>10){
  alert('用户名称最多10个字符(5个汉字)!');
  return false;
 }
 
 if(nameflag===false){
  alert('用户名称不能包含非法字符请更改!');
  return false;
 }
 //验证QQ号码
 var par =/^[1-9]d{4,12}$/;
 if(QQ.value!=''&&!par.test(QQ.value)){
  alert('请输入正确的QQ号码');
  return false;
 }
 //验证Email地址
 var emailpar = /^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+$/;
 if(Email.value!=''&&!emailpar.test(Email.value)){
  alert('请输入正确的邮箱地址!');
  return false;
 }
 if(QQ.value==''&&Email.value==''){
  alert('邮箱和QQ必选其一');
  return false;  
 }
 if(post.value==""){
  alert('请输入留言内容!');
  return false;   
 }
 if(post.value.length>400){
  alert('留言内容太长!');
  return false;   
 }

}
</script>
</head>

<body>
<table width="800" border="0" align="center">

  <tr>
    <td height="80" bgcolor="#003366"><span class="STYLE1"> 简易留言板教程(<a href="http://www.phpiask.com">PHP iask</a>)</span></td>
  </tr>
  <tr>
    <td height="5" bgcolor="#efefef"></td>
  </tr>
</table>
<table width="800" border="0" align="center" bgcolor="#fefefe">
<?php
while($rs=mysql_fetch_object($result)){
?>
  <tr>
    <td class="tdhx">留言人:<?php echo $rs->name?> |Email:<?php echo $rs->email?>|QQ:<?php echo $rs->qq?>|留言时间:<?php echo date("Y-m-d H:i:s",$rs->post_time+8*3600)?></td>
  </tr>
  <?php
  if(isset($_SESSION['login'])&&$_SESSION['login']){
  ?>
    <tr>
    <td class="tdhx"><a href="revert.php?id=<?php echo $rs->id?>">回复</a> | <a href="delete.php?id=<?php echo $rs->id?>">删除</a></td>
  </tr>
  <?php
  }
  ?>
  <tr>
    <td>留言内容:<?php echo nl2br(htmlspecialchars($rs->post))?><br/>
    <font color="Red">
    回复内容:<?php echo nl2br(htmlspecialchars($rs->revert))?>[<?php if($rs->revert_time!="") echo date("Y-m-d H:i:s",$rs->revert_time+8*3600)?> ]
    </font>
   
    </td>
  </tr>
  <tr><td height="3px"  bgcolor="##FF6600"></td></tr>
<?php
}
?>
</table>
<table width="800" border="0" align="center" bgcolor="#B1C3D9">
  <tr>
    <td >
<?php
if($page>0) echo "<a href='index.php?page=".($page-1)."'>上一页|</a>" ;
if($page<$totalpage-1) echo "<a href='index.php?page=".($page+1)."'>下一页</a>" ;
?></td>
  </tr>
</table><form action="post.php" method="post" id="postForm" name="postForm">
<table width="800" border="0" align="center" cellspacing="1" bgcolor="#efefef">
 
  <tr>
    <td width="117" bgcolor="#FFFFFF">姓名:</td>
    <td width="673" bgcolor="#FFFFFF"><label>
      <input type="text" name="name" id="name" />
    </label></td>
  </tr>
  <tr>
    <td bgcolor="#FFFFFF">Email:</td>
    <td bgcolor="#FFFFFF"><label>
      <input type="text" name="email" id="email" />
    </label></td>
  </tr>
  <tr>
    <td bgcolor="#FFFFFF">QQ:</td>
    <td bgcolor="#FFFFFF"><label>
      <input type="text" name="qq" id="qq"/>
    </label></td>
  </tr>
  <tr>
    <td colspan="2" bgcolor="#FFFFFF">留言内容:</td>
  </tr>
  <tr>
    <td colspan="2" bgcolor="#FFFFFF"><label>
      <textarea name="post" id="post" cols="40" rows="5"></textarea>
    </label></td>
  </tr>
  <tr>
    <td colspan="2" bgcolor="#FFFFFF"><label>
      <input type="submit" name="Submit" value="提交" onclick="return checkInput();"/>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input type="reset" name="Submit2" value="重置" />
    </label><a href="login.php">管理员登录</a></td>
  </tr>
</table></form>
</body>
</html>

post.php文件

<?php
header('content-type:text/html;charset=utf-8');
//如果PHP设置的自动转义函数未开启,就转义这些值
if(!get_magic_quotes_gpc()){
 foreach ($_POST as &$items){
  $items = addslashes($items);
 }
}

$name = $_POST['name'];
$qq = $_POST['qq'];
$email = $_POST['email'];
$post = $_POST['post'];

if($name==""||strlen($name)>10){
 echo <<<tem
 <script language="javascript">
 alert('请输入正确的有户名');
 history.go(-1);
 </script>
tem;
exit();
}
if($qq==""&&$email==""){
 echo <<<tem
 <script>
 alert('Email和QQ必须输入一个!');
 history.go(-1);
 </script>
tem;
exit();
}
if($qq!=""&&(!is_numeric($qq)||$qq>9999999999||$qq<=9999)){
 echo <<<tem
 <script>
 alert("请输入正确的QQ号码");
 history.go(-1);
 </script>
tem;
exit();
}
if($email!=""&&(!ereg("^([a-zA-Z0-9_-])[email protected]([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+",$email)||strlen($email)>60)){
 echo <<<tem
 <script>
 alert("请输入正确的Email");
 history.go(-1);
 </script>
tem;
exit();
}
if(strlen($post)>400){
 echo <<<tem
 <script>
 alert("输入的留言内容太长!");
 history.go(-1);
 </script>
tem;
exit();
}

//链接数据库
$con=mysql_connect('localhost','root','root') or die('链接数据库失败!');
mysql_query('set names utf8');
mysql_select_db('GuestBook');

//把客户信息插入guest表
$insertSql="insert into guest (name,qq,email) values ('$name','$qq','$email')";
if(mysql_query($insertSql)){
 $guestid = mysql_insert_id();
}
else{
 echo $insertSql;
 echo mysql_error();
 echo "数据插入失败!";
 exit();
}

//把以上插入取得的客户id和留言信息插入到post表中
$post_time = time();
$insertPostSql = "insert into post(guest_id,post,post_time) values('$guestid','$post','$post_time')";
if(mysql_query($insertPostSql)){
 echo <<<tem
 <script>
 alert("留言成功");
 location.href="index.php";
 </script>
tem;
}
else{
 echo <<<tem
 <script>
 alert("留言失败");
 location.href="index.php";
 </script>
tem;
}
?>

下面为后台管理管理的页面 login.php登录先

 

 代码如下 复制代码
<?php
session_start();
if(isset($_POST['Submit'])){
 if(!get_magic_quotes_gpc()){
  foreach ($_POST as &$items){
   $items = addslashes($items);
  }
 }
 if($_POST['username']=='phpiask'&&md5($_POST['password'])=='6dc88b87062a5de19895e952fa290dad'){
  $_SESSION['login']=true;
  echo "<script>alert('管理员登录成功');location.href='index.php';</script>";
  exit();
 }
 else {
  echo "<script>alert('登录失败!');</script>";
 }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<table>
<tr>
<td>
<form action="login.php" method="POST" name="form1">
用户名:<input type="text" name="username" size="20"/>
密码:<input type="password" name="password" size="20">
<input type="submit" value="登录" name="Submit"/>
<input type="button" onclick="javascript:location.href='index.php'" value="放弃"/>
</form>
</td>
</tr>
</table>
</body>
</html>

删除留言的delete.php

 代码如下 复制代码

<?php
session_start();
header('content-type:text/html;charset=utf-8');
$con=mysql_connect('localhost','root','root') or die('链接数据库失败!');
mysql_query('set names utf8');
mysql_select_db('GuestBook');

if(!$_SESSION['login']){
 echo "<script>alert('权限不足!');location.href='index.php';</script>";
 exit();
}

if(isset($_GET['id'])&&$_GET['id']!=""){
 $delRevertSql="delete from revert where post_id=".$_GET['id'];
 mysql_query($delRevertSql);
 
 $delGuestSql="delete from guest where id = (select guest_id from post where id=".$_GET['id'].")";
 mysql_query($delGuestSql);
 
 $delPostSql="delete from post where id=".$_GET['id'];
 mysql_query($delPostSql);
 
 if(mysql_error()==""){
  echo "<script>alert('删除成功!');location.href='index.php';</script>";
 }
}
?>

回复留言的revert.php文件

 代码如下 复制代码

<?php
session_start();
$con=mysql_connect('localhost','root','root') or die('链接数据库失败!');
mysql_query('set names utf8');
mysql_select_db('GuestBook');

if(!$_SESSION['login']){
 echo "<script>alert('没有登录不能回复!');location.href='index.php';</script>";
 exit();
}
if($_POST['Submit']){
 if(!get_magic_quotes_gpc()){
  foreach ($_POST as $items){
   $items = addslashes($items);
  }
 }
 if(strlen($_POST['revert'])>400){
  echo "<script>alert('回复内容过长!');history.go(-1);</script>";
  exit();
 }
 $post_id = $_POST['post_id'];
 $revert = $_POST['revert'];
 $insertRevertSql = "insert into revert (post_id,revert,revert_time) value('$post_id','$revert','$time')";
 if(mysql_query($insertRevertSql)){
  echo "<script>alert('回复成功');location.href='index.php';</script>";
  exit();
 }
 else {
  echo "<script>alert('回复失败!');history.go(-1);</script>";
 }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<table>
<tr>
<td>
<form action="revert.php" method="POST" name="form1">
回复内容:<textarea name="revert" cols="30" rows="5" id="revert"></textarea>
<input type="hidden" name="post_id" value="<?php echo $_GET['id']?> "size="20">
<input type="submit" value="回 复" name="Submit"/>
<input type="button" onclick="javascript:history.go(-1);" value="放弃"/>
</form>
</td>
</tr>
</table>
</body>
</html>

文章给你分享一篇简单的关于ubb代码如何利用php来解析哦,我们主要用到了preg_replace()函数,有了其它一切都好做
 代码如下 复制代码

/**
+----------------------------------------------------------
* UBB 解析
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
function ubb($Text) {
$Text=trim($Text);
$Text=ereg_replace("n","<br>",$Text);
$Text=preg_replace("/\t/is"," ",$Text);
$Text=preg_replace("/[hr]/is","<hr>",$Text);
$Text=preg_replace("/[separator]/is","<br/>",$Text);
$Text=preg_replace("/[h1](.+?)[/h1]/is","<h1>\1</h1>",$Text);
$Text=preg_replace("/[h2](.+?)[/h2]/is","<h2>\1</h2>",$Text);
$Text=preg_replace("/[h3](.+?)[/h3]/is","<h3>\1</h3>",$Text);
$Text=preg_replace("/[h4](.+?)[/h4]/is","<h4>\1</h4>",$Text);
$Text=preg_replace("/[h5](.+?)[/h5]/is","<h5>\1</h5>",$Text);
$Text=preg_replace("/[h6](.+?)[/h6]/is","<h6>\1</h6>",$Text);
$Text=preg_replace("/[center](.+?)[/center]/is","<center>\1</center>",$Text);
//$Text=preg_replace("/[url=([^[]*)](.+?)[/url]/is","<a href=\1 target='_blank'>\2</a>",$Text);
$Text=preg_replace("/[url](.+?)[/url]/is","<a href="\1" target='_blank'>\1</a>",$Text);
$Text=preg_replace("/[url=(http://.+?)](.+?)[/url]/is","<a href='\1' target='_blank'>\2</a>",$Text);
$Text=preg_replace("/[url=(.+?)](.+?)[/url]/is","<a href=\1>\2</a>",$Text);
$Text=preg_replace("/[img](.+?)[/img]/is","<img src=\1>",$Text);
$Text=preg_replace("/[imgs(.+?)](.+?)[/img]/is","<img \1 src=\2>",$Text);
$Text=preg_replace("/[color=(.+?)](.+?)[/color]/is","<font color=\1>\2</font>",$Text);
$Text=preg_replace("/[colorTxt](.+?)[/colorTxt]/eis","color_txt('\1')",$Text);
$Text=preg_replace("/[style=(.+?)](.+?)[/style]/is","<div class='\1'>\2</div>",$Text);
$Text=preg_replace("/[size=(.+?)](.+?)[/size]/is","<font size=\1>\2</font>",$Text);
$Text=preg_replace("/[sup](.+?)[/sup]/is","<sup>\1</sup>",$Text);
$Text=preg_replace("/[sub](.+?)[/sub]/is","<sub>\1</sub>",$Text);
$Text=preg_replace("/[pre](.+?)[/pre]/is","<pre>\1</pre>",$Text);
$Text=preg_replace("/[emot](.+?)[/emot]/eis","emot('\1')",$Text);
$Text=preg_replace("/[email](.+?)[/email]/is","<a href='mailto:\1'>\1</a>",$Text);
$Text=preg_replace("/[i](.+?)[/i]/is","<i>\1</i>",$Text);
$Text=preg_replace("/[u](.+?)[/u]/is","<u>\1</u>",$Text);
$Text=preg_replace("/[b](.+?)[/b]/is","<b>\1</b>",$Text);
$Text=preg_replace("/[quote](.+?)[/quote]/is","<blockquote>引用:<div style='border:1px solid silver;background:#EFFFDF;color:#393939;padding:5px' >\1</div></blockquote>", $Text);
$Text=preg_replace("/[code](.+?)[/code]/eis","highlight_code('\1')", $Text);
$Text=preg_replace("/[php](.+?)[/php]/eis","highlight_code('\1')", $Text);
$Text=preg_replace("/[sig](.+?)[/sig]/is","<div style='text-align: left; color: darkgreen; margin-left: 5%'><br><br>--------------------------<br>\1<br>--------------------------</div>", $Text);
return $Text;
}

关于preg_replace()函数语法

preg_replace函数基础与实例代码
//preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] ) 主题为匹配搜索模式,替换替换
/*
要搜索的模式。它可以是一个字符串或一个字符串数组。

电子修饰符使preg_replace函数()替代治疗后,适当引用作为参数是php教程代码进行替换。提示:请确保置换构成一个有效的php代码字符串,否则php将抱怨在包含preg_replace函数线()解析错误。

返回值

preg_replace函数()返回一个数组,如果这个问题的参数是一个数组或一个字符串,否则。

如果找到匹配,新问题会产生,否则将返回主题不变或null如果发生错误。


更多详细内容请查看:http://www.111cn.net/phper/php-function/33530.htm

本文章介绍解决XMLHttpRequest(Ajax)不能设置自定义的Referer办法的,我们主要是利用使用服务器作为代理.来处理,是基于curl的方法哦。

在PHP中, 使用我最喜欢的最强大的CURL,嘿嘿

 下面是在万网查询域名的实例代码

<?php

$dn = $_GET['dn']; // 域名, 不包括www
$ex = $_GET['ex']; // 顶级域名, 如 .com, .cn, 包括最前面的.
// 查询域名是否已经注册

 代码如下 复制代码


$url = 'http://pandavip.www.net.cn/check/check_ac1.cgi';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true); // POST
curl_setopt($ch, CURLOPT_POSTFIELDS, 'domain='.$dn.$ex);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0');
curl_setopt($ch, CURLOPT_COOKIE, '__utma=1.1486902564.1322109246.1322109246.1322109246.1; __utmz=1.1322109246.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); AdSource=GOOGLE%u641C%u7D22; AdWordID=gg96011009070005; __utmc=1');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'X-Requested-With' => 'XMLHttpRequest', // 设置为Ajax方式
    'Referer' => 'http://pandavip.www.net.cn/cgi-bin/Check.cgi?queryType=0&domain1='.$dn.'&image.x=0&image.y=0&domain='.$dn.'&big5=n&sign=2&url=www.net.cn&'.trim($ex, '.').'=yes' // 冒名顶替, 嘿嘿
));
curl_exec($ch); // 将查询结果返回前端, 用JS处理

在文章里面我说了很多关于php计算时间间隔实现程序代码的实现方法原理以及分析如何来做这个功能,有需用的同学可以仔细看看哦。

下面实例是告诉我们在论坛有看到关于多少秒钟前发了帖子等这些功能,

分析

实际时间 PHP

1秒 2“秒”

______________________

30秒==60

5分钟==60*10 //这里我觉得应该是五分钟,而不是一分钟,个人觉得 一分钟的话应该是 60*2

10分钟==60*20

30分钟==60*60

1小时==60*60*2

2.5小时==60*60*5


原理

用当前时间time(),减去你信息录入时的新增时间,(注意是时间戳),然后得出的差数,就是秒数,再根据自己的需要来转换啊。例如你想转换成分钟,就除以60。

实例

 代码如下 复制代码

<?php
/**
 * 时间差计算
 *
 * @param Timestamp $time
 * @return String Time Elapsed
 * @author Shelley Shyan
 * @copyright http://www.111cn.net (Professional PHP Architecture)
 */
function time2Units ($time)
{
   $year   = floor($time / 60 / 60 / 24 / 365);
   $time  -= $year * 60 * 60 * 24 * 365;
   $month  = floor($time / 60 / 60 / 24 / 30);
   $time  -= $month * 60 * 60 * 24 * 30;
   $week   = floor($time / 60 / 60 / 24 / 7);
   $time  -= $week * 60 * 60 * 24 * 7;
   $day    = floor($time / 60 / 60 / 24);
   $time  -= $day * 60 * 60 * 24;
   $hour   = floor($time / 60 / 60);
   $time  -= $hour * 60 * 60;
   $minute = floor($time / 60);
   $time  -= $minute * 60;
   $second = $time;
   $elapse = '';

   $unitArr = array('年'  =>'year', '个月'=>'month',  '周'=>'week', '天'=>'day',
                    '小时'=>'hour', '分钟'=>'minute', '秒'=>'second'
                    );

   foreach ( $unitArr as $cn => $u )
   {
       if ( $$u > 0 )
       {
           $elapse = $$u . $cn;
           break;
       }
   }

   return $elapse;
}

$past = 2052345678; // Some timestamp in the past
$now  = time();     // Current timestamp
$diff = $now - $past;

echo '发表于' . time2Units($diff) . '前';
?>

这是一个刚入门的同学写的

 代码如下 复制代码

<?php
    $regist1 = "05/12/2006";
    $regist2 = "10/05/2007";

    list($month1,$day1,$year1) = explode("/",$regist1);
    list($month2,$day2,$year2) = explode("/",$regist2);

    $regist1 = mktime(0,0,0,$month1,$day1,$year1);
    $regist2 = mktime(0,0,0,$month2,$day2,$year2);

    $time_difference = $regist2-$regist1;

    echo ("时间差:");
    echo date("Y",$time_difference) - 1970;
    echo ("年");
    echo date("m",$time_difference) - 1;
    echo ("个月");
    echo date("d",$time_difference) - 1;
    echo ("天");
?>


好了喜欢就自己选择用吧。

操作类操作svn