首页 > 编程技术 > php

收集了几款常用php日期操作

发布时间:2016-11-25 15:52

这里的日期操作函数有时间的增加,计算出给出的日期是星期几 ,检查日期是否合法日期  时间比较函数,返回两个日期相差几秒、几分钟、几小时或几天

<?php
function dateadd($date, $int, $unit = "d") {

//时间的增加(还可以改进成时分秒都可以增加,有时间再补上)
$datearr = explode("-", $date);
$value[$unit] = $int;

//

    return date("y-m-d", mktime(0,0,0, $datearr[1] + $value['m'], $datearr[2] + $value['d'], $datearr[0] + $value['y']));

//一聚教程网

}

function getweekday($date) {  //计算出给出的日期是星期几
$datearr = explode("-", $date);
    return date("w", mktime(0,0,0,$datearr[1],$datearr[2],$datearr[0]));

//

}
?>


<?
function check_date($date) { //检查日期是否合法日期
$datearr = explode("-", $date);
    if (is_numeric($datearr[0]) && is_numeric($datearr[1]) && is_numeric($datearr[2])) { //一聚教程网
        return checkdate($datearr[1],$datearr[2],$datearr[0]);
    }
    return false;
}
function check_time($time) {  //检查时间是否合法时间

//一聚教程网

$timearr = explode(":", $time);
    if (is_numeric($timearr[0]) && is_numeric($timearr[1]) && is_numeric($timearr[2])) {

//

        if (($timearr[0] >= 0 && $timearr[0] <= 23) && ($timearr[1] >= 0 && $timearr[1] <= 59) && ($timearr[2] >= 0 && $timearr[2] <= 59))

//php

            return true;
        else
            return false;
    }
    return false;
}


function datediff($date1, $date2, $unit = "") {

//时间比较函数,返回两个日期相差几秒、几分钟、几小时或几天
//
switch ($unit) {
        case 's':
$dividend = 1;
            break;
        case 'i':
$dividend = 60;

            break;
        case 'h':
$dividend = 3600;
            break;
        case 'd':
$dividend = 86400;
            break; //
        default:
$dividend = 86400;
    }
$time1 = strtotime($date1);
$time2 = strtotime($date2);
    if ($time1 && $time2) //
        return (float)($time1 - $time2) / $dividend;
    return false;
}
?>

取得客户端ip地址
<?

//开源代码

function getip(){
             if (getenv("http_client_ip") && strcasecmp(getenv("http_client_ip"), "unknown"))
$ip = getenv("http_client_ip"); //开源
             else if (getenv("http_x_forwarded_for") && strcasecmp(getenv("http_x_forwarded_for"), "unknown"))
$ip = getenv("http_x_forwarded_for"); //
             else if (getenv("remote_addr") && strcasecmp(getenv("remote_addr"), "unknown"))
$ip = getenv("remote_addr");
             else if (isset($_server[@#remote_addr@#]) && $_server[@#remote_addr@#] && strcasecmp($_server[@#remote_addr@#], "unknown"))

//

$ip = $_server[@#remote_addr@#];
else
$ip = "unknown";
             return($ip);
}
?>

判断邮箱地址
<?
function checkemail($inaddress)
{
return (ereg("^([a-za-z0-9_-])+@([a-za-z0-9_-])+(.[a-za-z0-9_-])+",$inaddress));
}
?>

<?
function getip() { //获取ip
if ($_server["http_x_forwarded_for"])
$ip = $_server["http_x_forwarded_for"]; //
    else if ($_server["http_client_ip"])
$ip = $_server["http_client_ip"];
    else if ($_server["remote_addr"])
$ip = $_server["remote_addr"]; //
    else if (getenv("http_x_forwarded_for"))
$ip = getenv("http_x_forwarded_for");
    else if (getenv("http_client_ip"))
$ip = getenv("http_client_ip"); //
    else if (getenv("remote_addr"))
$ip = getenv("remote_addr");
    else
$ip = "unknown";
    return $ip;
}
?>

//html表格的每行转为csv格式数组

<?
function get_tr_array($table) {

//php教程开源代码

$table = preg_replace("'<td[^>]*?>'si",'"',$table);
$table = str_replace("</td>",'",',$table);
$table = str_replace("</tr>","{tr}",$table); //开源代码osphp.com.cn
//去掉 html 标记 
$table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
//去掉空白字符  
$table = preg_replace("'([rn])[s]+'","",$table);


$table = str_replace(" ","",$table);
$table = str_replace(" ","",$table);
$table = explode(",{tr}",$table);

array_pop($table);
        return $table;
}
?>

//将html表格的每行每列转为数组,采集表格数据

<?
function get_td_array($table) {
$table = preg_replace("'<table[^>]*?>'si","",$table);

//osphp.com.cn

$table = preg_replace("'<tr[^>]*?>'si","",$table);
$table = preg_replace("'<td[^>]*?>'si","",$table);
$table = str_replace("</tr>","{tr}",$table); //开源代码osphp.com.cn
$table = str_replace("</td>","{td}",$table);
//去掉 html 标记 
$table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table); //osphp.com.cn开源
//去掉空白字符  
$table = preg_replace("'([rn])[s]+'","",$table);
$table = str_replace(" ","",$table);

 

$table = str_replace(" ","",$table);
$table = explode('{tr}', $table);
array_pop($table); //php开源代码
        foreach ($table as $key=>$tr) {
$td = explode('{td}', $tr);
array_pop($td);
$td_array[] = $td; //        }
        return $td_array;
}
?>

//返回字符串中的所有单词 $distinct=true 去除重复

<?
function split_en_str($str,$distinct=true) {
preg_match_all('/([a-za-z]+)/',$str,$match);


        if ($distinct == true) {
$match[1] = array_unique($match[1]);
        }
sort($match[1]); //osphp.com.cn
        return $match[1];
}
?>

//取得所有链接

<?
function get_all_url($code){ 
preg_match_all('/<as+href=["|']?([^>"' ]+)["|']?s*[^>]*>([^>]+)</a>/i',$code,$arr);  //osphp.com.cn
        return array('name'=>$arr[2],'url'=>$arr[1]); 
}
?>

这个类适用于配合数据库教程查询分页,和数组分页。下面有使用方法。
[php教程]
<?php
/*
* 名称: 分页类
* 介绍: 适用于数组分页和配合sql查询的分页
* 作者: idlion || moonfly (id_lion@hotmail.com)
* 创建时间: 2006-02-18
* 最后修改: 2007-05-24
*/

class pagebreak {
private $mtotalrowsnum = 0;   // 总信息行数
private $mcurpagenumber = 1;  // 当前所在页
private $mtotalpagesnum = 1;  // 总页数
private $mquerystring;     // 页面传递的数据(url?后的字符串)
private $mpagerowsnum = 20;  // 每页显示行数
private $mindexbarlength = 5;  // 索引条的页数
private $mindexbar = '';     // 页码索引条
private $mpageinfo = '';     // 分页信息
// 页码索引条样式
private $mnextbutton = "<font style="font-family:webdings">8</font>";
private $mprebutton = "<font style="font-family:webdings">7</font>";
private $mfirstbutton = "<font style="font-family:webdings">9</font>";
private $mlastbutton = "<font style="font-family:webdings">:</font>";
private $mcss教程indexbarcurpage = "font-weight:bold;color:#ff0000";
private $mcssindexbarpage = '';
// 分页信息样式
private $mcsspageinfonumfont = 'color:#ff0000';
private $mcsspageinfofont = '';

// 构造方法
public function __construct(&$rsqlquery, $userpagerowsnum='') {
  if( !is_array($rsqlquery) ) {
   $this->setdbpagebreak($rsqlquery, $userpagerowsnum);
  }
  else {
   $this->setarraypagebreak($rsqlquery, $userpagerowsnum);
  }
}

// 设置数据库型分页
private function setdbpagebreak(&$rsqlquery, $userpagerowsnum='') {
  $this->setdbtotalrowsnum($rsqlquery);
  $this->settotalpagesnum($userpagerowsnum);
  if( $this->mtotalpagesnum > 1 ) {
   $this->setcurpagenumber();
   $this->setsqlquery($rsqlquery);
   $this->setquerystring();
   $this->setindexbar();
   $this->setpageinfo();
  }
}

// 设置数组型分页
private function setarraypagebreak(&$rarray, $userpagerowsnum='', $usertotalrowsnum='') {
  $this->setarraytotalrowsnum($rarray, $usertotalrowsnum);
  $this->settotalpagesnum($userpagerowsnum);
  if( $this->mtotalpagesnum > 1 ) {
   $this->setcurpagenumber();
   $this->setarray($rarray);
   $this->setquerystring();
   $this->setindexbar();
   $this->setpageinfo();
  }
}

// 数据库型计算总行数
private function setdbtotalrowsnum($rsqlquery) {
  $this->mtotalrowsnum = mysql教程_num_rows( mysql_query($rsqlquery) );
}

// 数组型计算总行数
private function setarraytotalrowsnum($array) {
  $this->mtotalrowsnum = count($array);
}

// 计算总页数
private function settotalpagesnum($userpagerowsnum='') {
  if( $userpagerowsnum ) {
   $this->mpagerowsnum = $userpagerowsnum;
  }
  $this->mtotalpagesnum = (int)( floor( ($this->mtotalrowsnum-1)/$this->mpagerowsnum )+1 );
}

// 计算当前页数
private function setcurpagenumber() {
  if( $_get['cur_page'] ) {
   $this->mcurpagenumber = $_get['cur_page'];
  }
}

// 修正sql截取语句
private function setsqlquery(&$rsqlquery) {
  $start_number = ($this->mcurpagenumber-1)*$this->mpagerowsnum;
  $rsqlquery .= " limit ".$start_number.",".$this->mpagerowsnum;
}

// 修正截取后的array
private function setarray(&$rarray) {
  $start_number = ($this->mcurpagenumber-1)*$this->mpagerowsnum;
  $rarray = array_slice($rarray, $start_number, $this->mpagerowsnum);
}

// 修正 $_get 传递数据
private function setquerystring() {
  $query_string = $_server['query_string'];
  if ( $query_string == '' ) {
   $this->mquerystring = "?cur_page=";
  }
  else {
   $this->mquerystring = preg_replace("/&?cur_page=d+/", '', $query_string);
   $this->mquerystring = "?".$this->mquerystring."&cur_page=";
  }
}

// 设置页码索引条
private function getpageindex() {
  if( $this->mtotalpagesnum <= $this->mindexbarlength ) {
   $first_number = 1;
   $last_number = $this->mtotalpagesnum;
  }
  else {
   $offset = (int)floor($this->mindexbarlength/2);
   if( ($this->mcurpagenumber-$offset) <= 1 ) {
    $first_number = 1;
   }
   elseif( ($this->mcurpagenumber+$offset) > $this->mtotalpagesnum ) {
    $first_number = $this->mtotalpagesnum-$this->mindexbarlength+1;
   }
   else {
    $first_number = $this->mcurpagenumber-$offset;
   }
   $last_number = $first_number+$this->mindexbarlength-1;
  }
  $last_number;
  for( $i=$first_number; $i<=$last_number; $i++ ) {
   if( $this->mcurpagenumber == $i ) {
    $page_index .= "<font style='".$this->mcssindexbarcurpage."'>".$i."</font> ";
   }
   else {
    $page_index .= "<a href='".$this->mquerystring.$i."' style='".$this->mcssindexbarpage."'>".$i."</a> ";
   }
  }
  return $page_index;
}

// 设置页码索引条
private function setindexbar() {
  $this->mindexbar = $this->getnavfirstbutton();
  $this->mindexbar .= $this->getnavprebutton();
  $this->mindexbar .= $this->getpageindex();
  $this->mindexbar .= $this->getnavnextbutton();
  $this->mindexbar .= $this->getnavlastbutton();
}

// 得到页码索引条 首页按钮
private function getnavfirstbutton() {
  return "<a href='".$this->mquerystring."1'>".$this->mfirstbutton."</a> ";
}

// 得到页码索引条 上一页按钮
private function getnavprebutton() {
  if( $this->mcurpagenumber>1 ) {
   $pre_number = $this->mcurpagenumber-1;
  }
  else {
   $pre_number = 1;
  }
  return "<a href='".$this->mquerystring.$pre_number."'>".$this->mprebutton."</a> ";
}

// 得到页码索引条 下一页按钮
private function getnavnextbutton() {
  if( $this->mcurpagenumber<$this->mtotalpagesnum ) {
   $next_number = $this->mcurpagenumber+1;
  }
  else {
   $next_number = $this->mtotalpagesnum;
  }
  return "<a href='".$this->mquerystring.$next_number."'>".$this->mnextbutton."</a> ";
}

// 得到页码索引条 末页按钮
private function getnavlastbutton() {
  return "<a href='".$this->mquerystring.$this->mtotalpagesnum."'>".$this->mlastbutton."</a> ";
}

// 设置分页信息
private function setpageinfo() {
  $this->mpageinfo ="<font style='".$this->mcsspageinfofont."'>";
  $this->mpageinfo .= "共 <font style='".$this->mcsspageinfonumfont."'>".$this->mtotalrowsnum."</font> 条信息 | ";
  $this->mpageinfo .= "<font style='".$this->mcsspageinfonumfont."'>".$this->mpagerowsnum."</font> 条/页 | ";
  $this->mpageinfo .= "共 <font style='".$this->mcsspageinfonumfont."'>".$this->mtotalpagesnum."</font> 页 | ";
  $this->mpageinfo .= "第 <font style='".$this->mcsspageinfonumfont."'>".$this->mcurpagenumber."</font> 页";
  $this->mpageinfo .= "</font>";
}

// 取出页码索引条
public function getindexbar() {
  return $this->mindexbar;
}

// 取出分页信息
public function getpageinfo() {
  return $this->mpageinfo;
}

}
?>
[/php]

标签:[!--infotagslink--]

您可能感兴趣的文章: