首页 > 编程技术 > php

php mysql日期计算代码[datediff]

发布时间:2016-11-25 16:04

呵呵,下面代码有一点乱啊,是我在测试时做的,php mysql日期计算代码[datediff]也是我今天需要时才来试的喽.下面看看代码

function a($d){
$Date_1=date("Y-m-d");
 
$Date_2=$d;

$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]);

return round(($d1-$d2)/3600/24);
}

//echo $Days;

$d=date("Y-m-d H:i:s");
$sql ="Select * from wk_member where m_ip='127.0.0.1' and datediff(m_dtime,'$d')=0 ";
$result =mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($result)<5){
 echo 't';
}
while ($rs =mysql_fetch_array($result) ){
 echo $rs['id'];
 echo '<br>';
}

本站原创转载请注明 www.111cn.net

PHP 文件处理

当书面申请网页,它通常重要的是能够保存数据。一般的做法是两种方式之一:写入数据到一个文件,或保存在一个数据库(如MySQL的) 。当然,更容易的方法(不需要其他技术)是写入文件,然后读取并解析它们自己。这简单的这正是我在这里讨论。

因此,让我们开始吧:

打开一个文件的阅读和写作,我们开始使用打开功能。这只是开辟了档案资源为我们在以后的使用功能


<?php
 $filename = &apos;file.txt&apos;;
 $handle = fopen($filename, &apos;r&apos;);
?>



值得一提的是我打开文件进行读取。请注意' R '等?这是模式的论点,并允许您使用变量$文件阅读,或写作,这取决于你的需要。

你可以看到不同的模式,开辟了档案文件的功能(根据表1 ) 。
读取一个文件,最简单的方法是利用fread方法。这需要两个参数,档案资源从以前的功能(打开)和一个长度参数。


<?php
 $content = fread($handle, filesize($filename));
?>

我不得不使用文件大小,以便阅读文件的末尾。否则,该功能将不知道什么时候停止阅读。
写作到一个文件中做类似的方便。这一次,我们将只使用fwrite ,其中有两个要求论点:文件处理资源,该字符串写入(第三可选的参数是长度的内容写入) 。
<?php
 $handle = fopen($filename, &apos;a&apos;);
 fwrite($handle, &apos;Hello World!&apos;);
?>

我不得不重新处理美元的变量,因为以前的资源仅开放供读。
捷径
好吧,既然你知道如何打开,读,写的全部档案,很长的路,这里有一些捷径。我倾向于避免这些,但真的没有错用。请注意,这两种需要PHP5 。

而不是打开和fread ,可以简单地使用file_get_contents 。唯一的理由是需要的文件,它会返回一个字符串的内容
<?php
$contents = file_get_contents(&apos;file.txt&apos;);
?>

<?php
file_put_contents(&apos;file.txt&apos;, &apos;Hello World!&apos;);
?>

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

在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就也不难了.

这是一个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>";
 }
}
?>

标签:[!--infotagslink--]

您可能感兴趣的文章: