php、mysql查询当天,查询本周,查询本月的数据实例
php、mysql查询当天,查询本周,查询本月的数据实例(字段是时间戳)
代码如下 | 复制代码 |
//其中 video 是表名; //createtime 是字段; // //数据库time字段为时间戳 // //查询当天:
$start=date('Y-m-d 00:00:00'); $end=date('Y-m-d H:i:s'); SELECT * FROM `table_name` WHERE `time` >= unix_timestamp('$start') AND `time` <= unix_timestamp('$end')
//查询本周:
SELECT yearweek('2011-04-17 15:38:22',1 )//结果是201115 SELECT yearweek('2011-04-17 15:38:22')//结果是201116 //yearweek的第2个参数设置为1的原因是,中国人习惯把周1作为本周的第一天 //另外补充下: //2011-04-17 是周日。 SELECT dayofweek('2011-04-17 15:38:22')// 查询出的是1,把礼拜天作为一周的第一天。 SELECT dayofweek('2011-04-18 15:38:22')//查询出的是2 SELECT weekday('2011-04-17 15:38:22')// 查询出的是6, SELECT weekday('2011-04-18 15:38:22')// 查询出的是0, //所以建议使用weekday,查询出来的结果+1就可以了,就比较符合国人的习惯了。
SELECT * FROM `table_name` WHERE YEARWEEK( FROM_UNIXTIME( `time`,'%Y-%m-%d %H:%i:%s') ,1) = YEARWEEK( now( ),1 )
//查询本月:
$start=date('Y-m-01 00:00:00'); $end=date('Y-m-d H:i:s'); SELECT * FROM `table_name` WHERE `time` >= unix_timestamp('”.$start.”') AND `time` <= unix_timestamp('$end')
//查询本年:
$start=date('Y-01-01 00:00:00'); $end=date('Y-m-d H:i:s'); SELECT * FROM `table_name` WHERE `time` >= unix_timestamp('$start') AND `time` <= unix_timestamp('$end') |
php 获取今日、昨日、上周、本月的起始时间戳和结束时间
代码如下 | 复制代码 |
<?php //<!--php 获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法,主要使用到了 php 的时间函数 mktime()。-->
//1、php获取今日开始时间戳和结束时间戳
$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y')); $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
echo$beginToday.'---'.$endToday; echo'<br/>'; //2、php获取昨日起始时间戳和结束时间戳
$beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y')); $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
echo$beginYesterday.'---'.$endYesterday; echo'<br/>'; //3、php获取上周起始时间戳和结束时间戳
$beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y')); $endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));
echo$beginLastweek.'---'.$endLastweek; echo'<br/>';
//4、php获取本月起始时间戳和结束时间戳
$beginThismonth=mktime(0,0,0,date('m'),1,date('Y')); $endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
echo$beginThismonth.'---'.$endThismonth; echo'<br/>';
//PHP mktime() 函数用于返回一个日期的 Unix 时间戳。 //语法:mktime(hour,minute,second,month,day,year,is_dst) // //参数 描述 //hour 可选。规定小时。 //minute 可选。规定分钟。 //second 可选。规定秒。 //month 可选。规定用数字表示的月。 //day 可选。规定天。 //year 可选。规定年。在某些系统上,合法值介于 1901 - 2038 之间。不过在 PHP 5 中已经不存在这个限制了。 //is_dst可选。如果时间在日光节约时间(DST)期间,则设置为1,否则设置为0,若未知,则设置为-1。 //自 5.1.0 起,is_dst 参数被废弃。因此应该使用新的时区处理特性。参数总是表示 GMT 日期,因此 is_dst 对结果没有影响。 // //参数可以从右到左依次空着,空着的参数会被设为相应的当前 GMT 值。
echo(date("M-d-Y",mktime(0,0,0,12,36,2001)));
//将输出结果如: // //Jan-05-2002 |
*自己整理的一个水印类*
支持添加图片、文字、填充颜色区域
代码如下 | 复制代码 |
<?php /** * 图片加水印类,支持文字水印、透明度设置、自定义水印位置等。 * 使用示例: * $obj = new WaterMask($imgFileName); //实例化对象 * $obj->$waterType = 1; //类型:0为文字水印、1为图片水印 * $obj->$transparent = 45; //水印透明度 * $obj->$waterStr = 'icp.niufee.com'; //水印文字 * $obj->$fontSize = 18; //文字字体大小 * $obj->$fontColor = array(255,255,255); //水印文字颜色(RGB) * $obj->$fontFile = 'AHGBold.ttf'; //字体文件 * …… * $obj->output(); //输出水印图片文件覆盖到输入的图片文件 * @modify liuzp111 */ classWaterMask{ public$waterTypeImage = false; //水印类型:启用图片水印 public$waterTypeStr = false; //水印类型:启用文字水印 public$pos = 0; //水印位置 public$transparent = 45; //水印透明度(0---100)数值越大越不透明
public$waterStr ='icp.niufee.com'; //水印文字 public$fontSize = 14; //文字字体大小 public$fontColor =array(0,0,0); //水印文字颜色(RGB) 默认黑色 public$fontFile ='./font/simfang.ttf'; //字体文件
public$waterImg ='logo.png'; //水印图片
private$srcImg =''; //需要添加水印的图片 private$im =''; //图片句柄 private$water_im =''; //水印图片句柄 private$srcImg_info =''; //图片信息 private$waterImg_info =''; //水印图片信息 private$str_w =''; //水印文字宽度 private$str_h =''; //水印文字高度 private$x =''; //水印X坐标 private$y =''; //水印y坐标 public$output_img =''; //存储输出图片到哪里 public$is_draw_rectangle= false; //是否绘制矩形区域 (暂不支持自定义位置) //public $rectange_color = ''; //绘制矩形区域的颜色 private$result_array =array(); //结果数组 publicfunction__construct($img) { //析构函数 //$this->srcImg = file_exists($img) ? $img : die('"'.$img.'" 源文件不存在!'); if(file_exists($img)){ $this->srcImg =$img; }else{ returnarray('data'=>'','info'=>'源文件不存在!','status'=>0); } }
privatefunctionimginfo() { //获取需要添加水印的图片的信息,并载入图片。 $this->srcImg_info =getimagesize($this->srcImg); switch($this->srcImg_info[2]) { case3: $this->im = imagecreatefrompng($this->srcImg); break1; case2: $this->im = imagecreatefromjpeg($this->srcImg); break1; case1: $this->im = imagecreatefromgif($this->srcImg); break1; default: //die('原图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。'); returnarray('data'=>'','info'=>'原图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。','status'=>0); } }
privatefunctionwaterimginfo() { //获取水印图片的信息,并载入图片。 $this->waterImg_info =getimagesize($this->waterImg); switch($this->waterImg_info[2]) { case3: $this->water_im = imagecreatefrompng($this->waterImg); break1; case2: $this->water_im = imagecreatefromjpeg($this->waterImg); break1; case1: $this->water_im = imagecreatefromgif($this->waterImg); break1; default: //die('水印图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。'); returnarray('data'=>'','info'=>'水印图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。','status'=>0); } } privatefunctionwaterpos() { //水印位置算法 switch($this->pos) { case0: //随机位置 $this->x = rand(0,$this->srcImg_info[0]-$this->waterImg_info[0]); $this->y = rand(0,$this->srcImg_info[1]-$this->waterImg_info[1]); break1; case1: //上左 $this->x = 0; $this->y = 0; break1; case2: //上中 $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2; $this->y = 0; break1; case3: //上右 $this->x =$this->srcImg_info[0]-$this->waterImg_info[0]; $this->y = 0; break1; case4: //中左 $this->x = 0; $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2; break1; case5: //中中 $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2; $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2; break1; case6: //中右 $this->x =$this->srcImg_info[0]-$this->waterImg_info[0]; $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2; break1; case7: //下左 $this->x = 0; $this->y =$this->srcImg_info[1]-$this->waterImg_info[1]; break1; case8: //下中 $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2; $this->y =$this->srcImg_info[1]-$this->waterImg_info[1]; break1; case9: //下中偏上100px $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2; $this->y =$this->srcImg_info[1]-$this->waterImg_info[1] - 100; break1; default: //下右 $this->x =$this->srcImg_info[0]-$this->waterImg_info[0]; $this->y =$this->srcImg_info[1]-$this->waterImg_info[1]; break1; } } /** * 水印文字图片位置,根据需求调整 */ privatefunctionwaterposStr() { $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2; $this->y =$this->srcImg_info[1]-$this->waterImg_info[1] - 3; } privatefunctionwaterimg($type='') { if($this->srcImg_info[0] <=$this->waterImg_info[0] ||$this->srcImg_info[1] <=$this->waterImg_info[1]){ //die('水印比原图大!'); returnarray('data'=>'','info'=>'水印比原图大!','status'=>0); } if($type=='waterstr'){ $this->waterposStr(); }else{ $this->waterpos(); } $cut= imagecreatetruecolor($this->waterImg_info[0],$this->waterImg_info[1]); imagecopy($cut,$this->im,0,0,$this->x,$this->y,$this->waterImg_info[0],$this->waterImg_info[1]); $pct=$this->transparent; imagecopy($cut,$this->water_im,0,0,0,0,$this->waterImg_info[0],$this->waterImg_info[1]); imagecopymerge($this->im,$cut,$this->x,$this->y,0,0,$this->waterImg_info[0],$this->waterImg_info[1],$pct); }
privatefunctionwaterstr() { $rect= imagettfbbox($this->fontSize,0,$this->fontFile,$this->waterStr); $w=abs($rect[2]-$rect[6]); $h=abs($rect[3]-$rect[7]); $fontHeight=$this->fontSize; $this->water_im = imagecreatetruecolor($w,$h); imagealphablending($this->water_im,false); imagesavealpha($this->water_im,true); $white_alpha= imagecolorallocatealpha($this->water_im,255,255,255,127); imagefill($this->water_im,0,0,$white_alpha); $color= imagecolorallocate($this->water_im,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]); imagettftext($this->water_im,$this->fontSize,0,0,$this->fontSize,$color,$this->fontFile,$this->waterStr); $this->waterImg_info =array(0=>$w,1=>$h); $this->waterimg($type='waterstr'); } /** * 绘制矩形区 * bool imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color ) * bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col ) * @author liuzp111 */ publicfunctiondrawRectangle() { //imagefill($im,0,0,$gray);//填充资源,填充的坐标(类似PS魔棒),颜色 /* * 1--------------画长方形-------------- * bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col ) * 参数: 画布资源, 左上角x坐标,左上y坐标,右下x坐标,右下y坐标,颜色 */ $color= imagecolorallocate($this->im,255,255,255);//创建矩形边框颜色和填充颜色 //========================================================================= //绘制矩形区域并填充 // 参数说明: //bool imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color ) // im:为将图像载入为图像资源 // $x1:表示矩形左上角的X坐标 // $y1:表示矩形左上角的Y坐标 // $x2:表示矩形右下角的X坐标 // $y2:表示矩形右下角的Y坐标 // $color:为填充的RGB颜色 // imagefilledrectangle($this->im,3,$this->srcImg_info[1] - 20,$this->srcImg_info[0]-3,$this->srcImg_info[1]-3,$color); //不要使用下方的函数填充,下方填充函数为魔棒填充,容易导致填充不完整 //imagefill($this->im,$this->srcImg_info[0]/2,$this->srcImg_info[1]-8,$color);//填充资源,填充的坐标(魔棒),颜色
} functionoutput() { $this->imginfo(); //是否创建矩形区域 if($this->is_draw_rectangle){ $this->drawRectangle(); } if($this->waterTypeStr ) { $this->waterstr(); } if($this->waterTypeImage ) { $this->waterimginfo(); $this->waterimg(); } switch($this->srcImg_info[2]) { case3: $res_output= imagepng($this->im,$this->output_img); break1; case2: $res_output= imagejpeg($this->im,$this->output_img); break1; case1: $res_output= imagegif($this->im,$this->output_img); break1; default: // die('添加水印失败!'); returnarray('data'=>'','info'=>'添加水印失败!','status'=>0); break; } imagedestroy($this->im); imagedestroy($this->water_im); returnarray('data'=>$res_output,'info'=>'添加水印成功!','status'=>1); } } |
使用方式:
代码如下 | 复制代码 |
$file='58368dddc8c51_22';//需要加水印的图片 $file_ext='.jpeg';//扩展名 $imgFileName='./'.$file.$file_ext;//需要加水印图片路径 $obj=newWaterMask($imgFileName);//实例化对象 $obj->waterTypeStr = true; //开启文字水印 $obj->waterTypeImage = true; //开启图片水印 $obj->pos = 9; //定义水印图片位置 $obj->waterImg ='./water.png'; //水印图片 $obj->transparent = 100; //水印透明度 $obj->waterStr ='保险经纪人:刘测试 电话:02052552'; //水印文字 $obj->fontSize = 9; //文字字体大小 $obj->fontColor =array(0,0,0); //水印文字颜色(RGB) $obj->fontFile ='./font/msyh.ttc'; //字体文件,这里是微软雅黑 $obj->is_draw_rectangle = TRUE; //开启绘制矩形区域 $obj->output_img ='./'.$file.'_n'.$file_ext;//输出的图片路径 $obj->output(); |
本文实例讲述了php基于dom实现读取图书xml格式数据的方法。分享给大家供大家参考,具体如下:
代码如下 | 复制代码 |
<?php $doc=newDOMDocument(); $doc->load('books.xml'); $books=$doc->getElementsByTagName("book"); foreach($booksas$book) { $authors=$book->getElementsByTagName("author"); $author=$authors->item(0)->nodeValue; $publishers=$book->getElementsByTagName("publisher"); $publisher=$publishers->item(0)->nodeValue; $titles=$book->getElementsByTagName("title"); $title=$titles->item(0)->nodeValue; echo"$title - $author - $publisher\n"; } ?> |
books.xml文件如下:
代码如下 | 复制代码 |
<?xmlversion="1.0"?> <books> <book> <author>Jack Herrington</author> <title>PHP Hacks</title> <publisher>O'Reilly</publisher> </book> <book> <author>Jack Herrington</author> <title>Podcasting Hacks</title> <publisher>O'Reilly</publisher> </book> </books> |
运行结果如下:
PHP Hacks - Jack Herrington - O'Reilly
Podcasting Hacks - Jack Herrington - O'Reilly
小编推荐的这篇文章介绍了php基于dom实现的图书xml格式数据示例,非常实用,有兴趣的同学快来看看吧。代码如下 | 复制代码 |
<?php $books=array(); $books[] =array( 'title'=>'PHP Hacks', 'author'=>'Jack Herrington', 'publisher'=>"O'Reilly" ); $books[] =array( 'title'=>'Podcasting Hacks', 'author'=>'Jack Herrington', 'publisher'=>"O'Reilly" ); $doc=newDOMDocument(); $doc->formatOutput = true; $r=$doc->createElement("books"); $doc->appendChild($r); foreach($booksas$book) { $b=$doc->createElement("book"); $author=$doc->createElement("author"); $author->appendChild( $doc->createTextNode($book['author'] ) ); $b->appendChild($author); $title=$doc->createElement("title"); $title->appendChild( $doc->createTextNode($book['title'] ) ); $b->appendChild($title); $publisher=$doc->createElement("publisher"); $publisher->appendChild( $doc->createTextNode($book['publisher'] ) ); $b->appendChild($publisher); $r->appendChild($b); } echo$doc->saveXML(); ?> |
运行结果如下:
代码如下 | 复制代码 |
<?xmlversion="1.0"?> <books> <book> <author>Jack Herrington</author> <title>PHP Hacks</title> <publisher>O'Reilly</publisher> </book> <book> <author>Jack Herrington</author> <title>Podcasting Hacks</title> <publisher>O'Reilly</publisher> </book> </books> |
相关文章
- 本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
- 这篇文章主要介绍了Mybatis Plus select 实现只查询部分字段的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-01
- 最基础的对数据的增加删除修改操作实例,菜鸟们收了吧...2013-09-26
- 这篇文章主要介绍了解决Mybatis 大数据量的批量insert问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-09
Antd-vue Table组件添加Click事件,实现点击某行数据教程
这篇文章主要介绍了Antd-vue Table组件添加Click事件,实现点击某行数据教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-17- 这篇文章主要介绍了详解如何清理redis集群的所有数据,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-18
- 这篇文章主要介绍了vue 获取到数据但却渲染不到页面上的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-19
- 在php中解析xml文档用专门的函数domdocument来处理,把json在php中也有相关的处理函数,我们要把数据xml 数据存到一个数据再用json_encode直接换成json数据就OK了。...2016-11-25
- 这篇文章主要介绍了mybatis-plus 处理大数据插入太慢的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-18
MyBatisPlus-QueryWrapper多条件查询及修改方式
这篇文章主要介绍了MyBatisPlus-QueryWrapper多条件查询及修改方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2022-06-27- 这篇文章主要介绍了postgresql数据添加两个字段联合唯一的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-04
- 这篇文章主要介绍了Oracle使用like查询时对下划线的处理方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-16
- 这篇文章主要介绍了解决mybatis-plus 查询耗时慢的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-07-04
Vue生命周期activated之返回上一页不重新请求数据操作
这篇文章主要介绍了Vue生命周期activated之返回上一页不重新请求数据操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-26- 这篇文章主要介绍了c# socket网络编程,server端接收,client端发送数据,大家参考使用吧...2020-06-25
- 这篇文章主要介绍了vue 数据(data)赋值问题的解决方案,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-29
- 这篇文章主要介绍了Python3 常用数据标准化方法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-24
- 这篇文章主要介绍了解决vue watch数据的方法被调用了两次的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-07
- 如果需要在查询语句返回的列中包含一列表示该条记录在整个结果集中的行号, ISO SQL:2003 标准提出的方法是提供 ROW_NUMBER() / RANK() 函数。 Oracle 中可以使用标准方法(8i版本以上),也可以使用非标准的 ROWNUM ; MS SQL...2015-03-15
- 这篇文章主要为大家详细介绍了Node实现搜索框进行模糊查询,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-06-28