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

 更新时间:2016年11月25日 15:52  点击:1747

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

<?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--]

相关文章

  • php svn操作类

    以前我们开发大型项目时都会用到svn来同步,因为开发产品的人过多,所以我们会利用软件来管理,今天发有一居然可以利用php来管理svn哦,好了看看吧。 代码如下 ...2016-11-25
  • C#操作config文件的具体方法

    这篇文章介绍了在C#中对config文件的操作,有需要的朋友可以参考一下...2020-06-25
  • python自动化办公操作PPT的实现

    这篇文章主要介绍了python自动化办公操作PPT的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-05
  • 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
  • php计算两个日期相差天数的方法

    本文实例讲述了php计算两个日期相差天数的方法。...2015-03-15
  • PostgreSQL 字符串处理与日期处理操作

    这篇文章主要介绍了PostgreSQL 字符串处理与日期处理操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-01
  • vue开发之moment的介绍与使用

    moment是一款多语言支持的日期处理类库, 在vue中如何使用呢?这篇文章主要给大家介绍了关于vue之moment使用的相关资料,需要的朋友可以参考下...2021-05-13
  • nodejs文件操作模块FS(File System)常用函数简明总结

    件系统操作相关的函数挺多的。首先可以分为两大类。一类是异步+回调的。 一类是同步的。在这里只对异步的进行整理,同步的只需要在函数名称后面加上Sync即可1. 首先是一类最常规的读写函数,函数名称和形式,应该是起源于C...2014-06-07
  • 非常全面的php日期时间运算汇总

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

    这篇文章主要介绍了C#模拟window操作鼠标的方法,可实现模拟鼠标移动到固定位置后点击右键的功能,涉及鼠标常用事件的操作技巧,需要的朋友可以参考下...2020-06-25
  • C#实现将字符串转换成日期格式的方法

    这篇文章主要介绍了C#实现将字符串转换成日期格式的方法,涉及C#操作时间及字符串的相关技巧,非常简单实用,需要的朋友可以参考下...2020-06-25
  • 微信小程序手势操作之单触摸点与多触摸点

    这篇文章主要介绍了微信小程序手势操作之单触摸点与多触摸点的相关资料,需要的朋友可以参考下...2017-03-13
  • php根据日期或时间戳获取星座信息和生肖等信息

    分享一个利用php根据日期或时间戳获取相应的干支纪年,生肖和星座信息的函数方法,具体函数代码以及使用方法如下: /** 判断干支、生肖和星座 */ function birthext($birth){ if(strstr($birth,'-')===false&&strlen($bi...2015-10-21
  • python中字符串最常用的十三个处理操作记录

    这篇文章主要给大家介绍了关于python中字符串最常用的13个处理操作的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-09
  • java8时间 yyyyMMddHHmmss格式转为日期的代码

    这篇文章主要介绍了java8时间 yyyyMMddHHmmss格式转为日期的代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-17
  • C#判断日期是否到期的方法

    这篇文章主要介绍了C#判断日期是否到期的方法,是C#程序设计中非常实用的技巧,需要的朋友可以参考下...2020-06-25
  • js操作XML文件的实现方法兼容IE与FireFox

    下面小编就为大家带来一篇js操作XML文件的实现方法兼容IE与FireFox。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-07-01
  • C# 模拟浏览器并自动操作的实例代码

    这篇文章主要介绍了C# 模拟浏览器并自动操作的实例代码,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-11-03
  • C#比较日期的方法总结

    在本篇内容中小编给大家整理了关于C#比较日期的方法和相关知识点,有需要的朋友们学习下。...2020-06-25
  • c#对字符串操作的技巧小结

    c#对字符串操作的技巧小结,需要的朋友可以参考一下...2020-06-25