php日期相加减

 更新时间:2016年11月25日 16:04  点击:1344

在asp,.net中日期相加减是相当简单的一个datediff函数就可以算出两个日期的相差多长的时间了,而当我学php时以为也是这样,但完全与我想的不同,下面是我看到一个朋友写的php 日期相加减

<?PHP 
$Date_1="2008-8-15";//格式也可以是:$Date_1="2003-6-25 23:29:14";
 
$Date_2="2009-10-1"; 

$Date_List_a1=explode("-",$Date_1);
 
$Date_List_a2=explode("-",$Date_2); 

$d1=mktime(0,0,0,$Date_List_a1[1],$Date_List_a1[2],$Date_List_a1[0]); 

$d2=mktime(0,0,0,$Date_List_a2[1],$Date_List_a2[2],$Date_List_a2[0]); 

$Days=round(($d1-$d2)/3600/24); 

Echo "两日期之前相差有$Days 天"; 
?>
哈哈,只要我们会使用时间N就也不难了.

php 文件计数据器+mysql双保护,这是上次升级版了,上次流量大太经常出问题今天修改过来了.

<?php
 require_once("../inc/connect.php");
 Class Sit_count{
   public $FileName = 'count.txt'; 
  function Count_add(){
   if(file_exists($this->FileName) ){
    $Temp = file_get_contents($this->FileName);    
    $Array = explode("=",$Temp); 
    if(count($Array) != 2){   
     $Tmp ='mytext='.$this->Tcount();
    }else{
     $Total = $Array[1]+1;  
     $Tmp =$Array[0].'='.$Total;      
    }
    $T = @fopen($this->FileName,'w');
    if($T){
     fwrite($T,$Tmp);
     fclose($T);
    }
    mysql_query("update wk_count set count_bak =count_bak+1 where id=1");
   }else{
    echo '! file_exists';
   }
   
  }
 
  function Tcount(){
    $result =mysql_query("Select * from wk_count where id =1 ") or die(mysql_error());
   $rs =mysql_fetch_array($result);
   return $rs['count_bak'];
   
  }
 }
  $tp = new Sit_count;
  $tp->Count_add();
?>
本站原创转载注明www.111cn.net/phper/php.html

这是一个xml 解析类

<?php
class Message_XML extends DOMDocument
{
 const file_name = "e:/myphp/xmldom/xml/message.xml";
 private $root; //根节点
 private $PageNo; //当前页
 private $allNum; //记录总数
 private $PageSize; //页大小
 private $allPages; //总页数
 public function __construct()
 {
   parent::__construct();
   if(!file_exists(self::file_name))
   {
    $xmlStr = "<?xml version='1.0' encoding='utf-8' ?><root />";
    $this -> loadXML($xmlStr);
    $this -> save(self::file_name);
   }else{
    $this -> load(self::file_name);
   }
   $this -> root = $this -> documentElement;
   $this -> get_pagemsg();
 }
 //得到页信息
 private function get_pagemsg()
 {
   $this -> PageSize = 3; //页大小
   $allNode = $this -> getElementsByTagName("record");
   $this -> allNum = $allNode -> length; //记录总数
   $this -> allPages = ceil($this -> allNum / $this -> PageSize); //总页数
   $this -> PageNo = $_GET["PageNo"];
   if($this -> PageNo < 1 || !is_numeric($this -> PageNo))
   {
    $this -> PageNo = 1;
   }else if($this -> PageNo > $this -> allPages){
    $this -> PageNo = $this -> allPages;
   }
   $this -> PageNo = (int)$this -> PageNo;
 }
 //显示留言
 public function show_message()
 {
   $start_num = ($this -> PageNo - 1) * $this -> PageSize; //记录开始数
   $end_num = $start_num + $this -> PageSize - 1; //记录结束数
   $allNode = $this -> getElementsByTagName("record");
   $i = 0;
   foreach($allNode as $v)
   {
    if($i >= $start_num && $i <= $end_num)
    {
     $autoid = $v -> getElementsByTagName("autoid") -> item(0) -> nodeValue;
     $subject = $v -> getElementsByTagName("subject") -> item(0) -> nodeValue;
     $content = $v -> getElementsByTagName("content") -> item(0) -> nodeValue;
     $str = "<div class='msgInfo'><p class='msgT'><span>留言标题:</span>$subject</p><p class='msgC'><span>留言内容:</span><br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$content</p>";
     $str .= "<p class='msgCMD'><a href='?Action=edit_message&AutoID=$autoid&PageNo=$_GET[PageNo]'>编辑</a> <a href='?Action=delete_message&AutoID=$autoid&PageNo=$_GET[PageNo]'>删除</a></p></div>";
     print $str;
    }
    $i++;
   }
   $this -> get_pageCode();
 }
 //得到当前页码
 public function get_pageCode()
 {
  $str = "<div class='pageCode'>当前页:".$this -> PageNo." / ".$this -> allPages."&nbsp;&nbsp;&nbsp;<a href='?PageNo=1'>首页</a>&nbsp;<a href='?PageNo=".($this->PageNo - 1)."'>上一页</a>&nbsp;<a href='?PageNo=".($this->PageNo + 1)."'>下一页</a>&nbsp;<a href='?PageNo=".($this->allPages)."'>末页</a>";
  $str .= "&nbsp;&nbsp;&nbsp;<input type='text' size='2' id='goPage' value='".$this->PageNo."'><input type='button' value='GO' onclick=window.location='?PageNo='+document.getElementById('goPage').value>";
  print $str;
 }
 //添加留言页面
 public function post_message()
 {
   print "<div><form method='post' action='?Action=add_message&PageNo=$_GET[PageNo]'>";
   print "<p>标题:<input type='text' name='Subject' size='50' /></p>";
   print "<p>内容:<textarea name='Content' cols='50' rows='5'></textarea></p>";
   print "<p><input type='submit' value='添加留言'></p></div></form>";
 }
 //添加留言
 public function add_message($Subject,$Content)
 {
   $autoid = microtime();  //留言ID
   $autoid = substr(strrchr(str_replace(" ","",$autoid),"."),1);
   $node_top = $this -> root ->appendChild($this -> createElement("record"));
   $node_top -> appendChild($this -> createElement("autoid",$autoid));
   $node_top -> appendChild($this -> createElement("subject",$Subject));
   $node_top -> appendChild($this -> createElement("content",$Content));
   $this -> save(self::file_name);
   echo "<script>alert('添加留言成功!');window.location='".$_SERVER['PHP_SELF']."?PageNo=".$_GET['PageNo']."'</script>";
 }
 //清空留言
 public function clear_message()
 {
   $fp = @ fopen(self::file_name,"w+");
   if($fp)
   {
    $str = "<?xml version='1.0' encoding='utf-8' ?><root />";
    fwrite($fp,$str);
    fclose($fp);
    echo "<script>alert('清空成功!');window.location='".$_SERVER['PHP_SELF']."'</script>";
   }else{
    echo "<script>alert('清空失败!');history.back();</script>";
   }
 }
 //设置节点路径和操作对象ID
 private function set_nodePath($AutoID)
 {
   $xpath = new DOMXPath($this);
   $node_top = $xpath -> query("//record[autoid=$AutoID]");
   return $node_top;
 }
 //删除留言
 public function delete_message($AutoID)
 {
   $node_top = $this -> set_nodePath($AutoID);
   $this -> root -> removeChild($node_top -> item(0));
   $this -> save(self::file_name);
   echo "<script>alert('删除成功!');location='".$_SERVER['PHP_SELF']."?PageNo=".$_GET['PageNo']."'</script>";
 }
 //编辑留言页面
 public function edit_message($AutoID)
 {
   $node_top = $this -> set_nodePath($AutoID);
   //取值方法1
   //$subject = $node_top -> item(0) -> getElementsByTagName("subject") -> item(0) -> nodeValue;
   //$content = $node_top -> item(0) -> getElementsByTagName('content') -> item(0) ->nodeValue;
   //取值方法2
   foreach($node_top -> item(0) -> childNodes as $v)
   {
    $value[] = $v -> textContent; //注意:这里的$value必须这样写成一个数组,否则要出错
   }
   print "<div><form method='post' action='?Action=save_message&AutoID=$AutoID&PageNo=$_GET[PageNo]'>";
   print "<p>标题:<input type='text' name='Subject' value=$value[1] size='50' /></p>";
   print "<p>内容:<textarea name='Content' cols='50' rows='5'>$value[2]</textarea></p>";
   print "<p><input type='submit' value='编辑留言'></p></div></form>"; 
 }
 //编辑留言
 public function save_message($AutoID,$Subject,$Content)
 {
   $node_top = $this -> set_nodePath($AutoID);
   $replace_info[0] = $AutoID;
   $replace_info[1] = $Subject;
   $replace_info[2] = $Content;
   $i = 0;
   foreach($node_top -> item(0) -> childNodes as $v)
   {
    $new_content = $this -> createTextNode($replace_info[$i]);
    $v -> replaceChild($new_content,$v -> lastChild);
    $i++;
   }
   $this -> save(self::file_name);
   echo "<script>alert('编辑成功!');location='".$_SERVER['PHP_SELF']."?PageNo=".$_GET['PageNo']."'</script>";
 }
}
?>

今天没事,公司也要写一个php 解析xml的程序,在网上看看了,觉得这个不错就写出来和各位分享吧,我们先看看看留言页面吧.

<?php
include_once("class.php");
$HawkXML = new Message_XML();
$Action = $_GET["Action"];
?>
<html>
<head>
<meta http-equiv="Content-Language" content="en" />
<meta name="GENERATOR" content="Zend Studio" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body{
 margin: 0;
 padding: 0;
 text-align: center;
 color: #666;
}
div{
 margin: 0 auto;
 width: 800px;
}
p,h1,h2,h3,h4,h5,h6{margin: 0; padding:0}
a{text-decoration: none; color: #0033FF}
a:hover{text-decoration: underline}
span{
 font-weight: bold;
}
.top{
 margin-top: 15px;
 height: 35px;
 background: #00FFFF;
 line-height: 35px;
}
.msg{
 height: 25px;
 text-align: left;
 margin: 8px;
}
.msgT,.msgC{
 text-align: left;
 word-break:break-all;
}
.msgCMD{
 margin-top: 10px;
 text-align: right;
}
.msgInfo{
 margin: 3px 0;
 border: 1px solid #ccc;
}
.pageCode{
 margin: 3px 0;
 height: 35px;
 line-height: 35px;
 background: #eee;
}
</style>
<title>php+xml留言本</title>
</head>
<body>
<div class="top">php+xml留言本</div>
<div class="msg"><a href="?Action=post_message&PageNo=<?= $_GET['PageNo'] ?>">[发表留言]</a> <a href="?Action=show_message&PageNo=<?= $_GET['PageNo'] ?>">[显示留言]</a> [<a href="?Action=clear_message">清空留言</a>]</div>
<!--显示留言-->
<div clas="showmsg">
<?php
switch($Action)
{
 case "show_message": //显示留言
  $HawkXML -> show_message();
  break;
 case "post_message": //添加留言提交
  $HawkXML -> post_message();
  break;
 case "add_message": //添加留言
  $HawkXML -> add_message($_POST['Subject'],$_POST['Content']);
  break;
 case "clear_message":
  $HawkXML -> clear_message();
  break;
 case "delete_message":  //删除留言
  $AutoID = $_GET['AutoID'];
  $HawkXML -> delete_message($AutoID);
  break;
 case "edit_message": //修改留言
  $AutoID = $_GET['AutoID'];
  $HawkXML -> edit_message($AutoID);
  break;
 case "save_message": //修改留言提交
  $AutoID = $_GET['AutoID'];
  $HawkXML -> save_message($AutoID,$_POST['Subject'],$_POST['Content']);
  break;
 default: //默认显示留言
  $HawkXML -> show_message();
  break;
}
?>
</div>
</body>
</html>

最后我们把xml文档格式贴出来吧.

<?xml version="1.0" encoding="utf-8"?>
<root><record><autoid>265634001218784833</autoid><subject>测试留言1</subject><content>测试留言1测试留言1</content></record><record><autoid>140627001218784843</autoid><subject>测试留言2</subject><content>测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2</content></record><record><autoid>078128001218784850</autoid><subject>测试留言3</subject><content>测试留言3测试留言3测试留言3</content></record><record><autoid>765628001218784857</autoid><subject>测试留言4</subject><content>测试留言4测试留言4测试留言4测试留言4测试留言4测试留言4测试留言4测试留言4测试留言4</content></record></root>

ajax 注册验证,今天这个文章有一点怪了,就是验证url 是否能被正常访问哦,

<tr>
    <td height="34" align="center">博客地址:</td>
    <td height="25" align="center"><span class="STYLE1">*</span></td>
    <td><label>
    <input name="blog" type="text" id="blog" size="47" onblur="ajax(0);"  />
    </label></td>
    <td  id="u_info"></td>
  </tr>

上面是简单的html代码程序.下面贴出php 验证代码.

if( $ta==0){
 if(@file_get_contents($xm)){
  echo '<div id="success">&nbsp;&nbsp;&nbsp;&nbsp;博客地址可以访问</div>';
 }else{
  echo '<div id="error">对不起,你输入的博客地址不能正常访问<div >';
 }
}else{
 echo "<div id='error'>对不起,无效的操作!</div>";
}

好了我还是反ajax 代码也写出来吧,

var xmlHttp = false;

function ajaxcreate(){

try {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
try {
  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
 } catch (e2) {
  xmlHttp = false;
 }
 }

if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
  xmlHttp = new XMLHttpRequest();
 }
if(!xmlHttp){alert('Create Xmlhttp Fail ');return false;}
}

ajaxcreate();
  xmvalue=document.getElementById("blog").value;
 var url="ajax_check.php?txt="+encodeURI(xmvalue)+"&tag="+encodeURI(str)+"&rd="+Math.random();
 if (xmvalue== null || xmvalue.length>200 || xmvalue == "") return false;
 xmlHttp.open("POST",url,true);
 xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
 xmlHttp.send(xmvalue); 
 xmlHttp.onreadystatechange=returnstate; 
}
function returnstate(){
 if(xmlHttp.readyState != 4 ){
  document.getElementById("u_info").innerHTML="<div class=defult>正在验证请稍后...</div>";
 }
 if(xmlHttp.readyState == 4 ){
  document.getElementById("u_info").innerHTML=xmlHttp.responseText;
  
 }
}

OK下面这段我以前写过了,不过为了方便还是也写出来了,最后申明一下:本站原创文章转载请注明来:  www.111cn.net/phper/php.html

[!--infotagslink--]

相关文章

  • 源码分析系列之json_encode()如何转化一个对象

    这篇文章主要介绍了源码分析系列之json_encode()如何转化一个对象,对json_encode()感兴趣的同学,可以参考下...2021-04-22
  • php中去除文字内容中所有html代码

    PHP去除html、css样式、js格式的方法很多,但发现,它们基本都有一个弊端:空格往往清除不了 经过不断的研究,最终找到了一个理想的去除html包括空格css样式、js 的PHP函数。...2013-08-02
  • index.php怎么打开?如何打开index.php?

    index.php怎么打开?初学者可能不知道如何打开index.php,不会的同学可以参考一下本篇教程 打开编辑:右键->打开方式->经文本方式打开打开运行:首先你要有个支持运行PH...2017-07-06
  • JS日期加减,日期运算代码

    一、日期减去天数等于第二个日期function cc(dd,dadd){//可以加上错误处理var a = new Date(dd)a = a.valueOf()a = a - dadd * 24 * 60 * 60 * 1000a = new Date(a)alert(a.getFullYear() + "年" + (a.getMonth() +...2015-11-08
  • PostgreSQL 字符串处理与日期处理操作

    这篇文章主要介绍了PostgreSQL 字符串处理与日期处理操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-01
  • php计算两个日期相差天数的方法

    本文实例讲述了php计算两个日期相差天数的方法。...2015-03-15
  • 非常全面的php日期时间运算汇总

    实例讲解之前,先来介绍几个核心函数: mktime 函数 mktime() 函数返回一个日期的 Unix 时间戳。 参数总是表示 GMT 日期,因此 is_dst 对结果没有影响。 参数可以从右到左依次空着,空着的参数会被设为相应的当前 GMT 值。...2015-11-08
  • vue开发之moment的介绍与使用

    moment是一款多语言支持的日期处理类库, 在vue中如何使用呢?这篇文章主要给大家介绍了关于vue之moment使用的相关资料,需要的朋友可以参考下...2021-05-13
  • PHP中func_get_args(),func_get_arg(),func_num_args()的区别

    复制代码 代码如下:<?php function jb51(){ print_r(func_get_args()); echo "<br>"; echo func_get_arg(1); echo "<br>"; echo func_num_args(); } jb51("www","j...2013-10-04
  • PHP编程 SSO详细介绍及简单实例

    这篇文章主要介绍了PHP编程 SSO详细介绍及简单实例的相关资料,这里介绍了三种模式跨子域单点登陆、完全跨单点域登陆、站群共享身份认证,需要的朋友可以参考下...2017-01-25
  • C#实现将字符串转换成日期格式的方法

    这篇文章主要介绍了C#实现将字符串转换成日期格式的方法,涉及C#操作时间及字符串的相关技巧,非常简单实用,需要的朋友可以参考下...2020-06-25
  • PHP实现创建以太坊钱包转账等功能

    这篇文章主要介绍了PHP实现创建以太坊钱包转账等功能,对以太坊感兴趣的同学,可以参考下...2021-04-20
  • php根据日期或时间戳获取星座信息和生肖等信息

    分享一个利用php根据日期或时间戳获取相应的干支纪年,生肖和星座信息的函数方法,具体函数代码以及使用方法如下: /** 判断干支、生肖和星座 */ function birthext($birth){ if(strstr($birth,'-')===false&&strlen($bi...2015-10-21
  • php微信公众账号开发之五个坑(二)

    这篇文章主要为大家详细介绍了php微信公众账号开发之五个坑,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-10-02
  • java8时间 yyyyMMddHHmmss格式转为日期的代码

    这篇文章主要介绍了java8时间 yyyyMMddHHmmss格式转为日期的代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-17
  • ThinkPHP使用心得分享-ThinkPHP + Ajax 实现2级联动下拉菜单

    首先是数据库的设计。分类表叫cate.我做的是分类数据的二级联动,数据需要的字段有:id,name(中文名),pid(父id). 父id的设置: 若数据没有上一级,则父id为0,若有上级,则父id为上一级的id。数据库有内容后,就可以开始写代码,进...2014-05-31
  • PHP如何通过date() 函数格式化显示时间

    这篇文章主要介绍了PHP如何通过date() 函数格式化显示时间,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-11-13
  • PHP+jQuery+Ajax实现多图片上传效果

    今天我给大家分享的是在不刷新页面的前提下,使用PHP+jQuery+Ajax实现多图片上传的效果。用户只需要点击选择要上传的图片,然后图片自动上传到服务器上并展示在页面上。...2015-03-15
  • C#判断日期是否到期的方法

    这篇文章主要介绍了C#判断日期是否到期的方法,是C#程序设计中非常实用的技巧,需要的朋友可以参考下...2020-06-25
  • sql通过日期判断年龄函数的示例代码

    这篇文章主要介绍了sql通过日期判断年龄函数,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-07-16