php日期函数问题

 更新时间:2016年11月25日 15:13  点击:1295
php日期函数问题

<?php
// returns timestamp for 12:25:23 9-Jul-2006
echo gmmktime(12,25,23,7,9,2006);
?>

gmdate($format, $ts)

此函数将UNIX时间标签格式化成可人为阅读的日期字符串。此日期字符串以GMT(非当地时间)表示。

用GMT表示时间标签时应用此函数。

 

<?php
// get date as associative array
$arr = getdate();
echo "Date is " . $arr['mday'] . " " . $arr['weekday'] . " " . $arr['year'];
echo "Time is " . $arr['hours'] . ":" . $arr['minutes'];
?>

mktime($hour, $minute, $second, $month, $day, $year)

此函数的作用与getdate()的作用相反:它由一系列的日期与时间值生成一个UNIX时间标签(GMT时间1970年1月1日到现在消逝的秒数)。不用自变量时,它生成当前时间的UNIX时间标签。

用此函数获得即时时间的UNIX时间标签。这种时间标签通常用于许多数据库与程序语言中。


<?php
// returns timestamp for 13:15:23 7-Jun-2006
echo mktime(13,15,23,6,7,2006);
?>

date($format, $ts)

此函数将UNIX时间标签格式化成一个可人为阅读的日期字符串。它是PHP日期/时间API中功能最为强大的函数,可用在一系列的修正值中,将整数时间标签转变为所需的字符串格式。

为显示格式化时间或日期时,应用此函数。


<?php
// format current date
// returns "13-Sep-2005 01:16 PM"
echo date("d-M-Y h:i A", mktime());
?>

strtotime($str)

此函数将可人为阅读的英文日期/时间字符串转换成UNIX时间标签。

应用此函数将非标准化的日期/时间字符串转换成标准、兼容的UNIX时间标签。


<?php
// returns 13-Sep-05
echo date("d-M-y", strtotime("today"));
// returns 14-Sep-05
echo date("d-M-y", strtotime("tomorrow"));
// returns 16-Sep-05
echo date("d-M-y", strtotime("today +3 days"));
?>

strftime($format,$ts)

如前面的setlocale()函数定义的那样,此函数将UNIX时间标签格式化成适用于当前环境的日期字符串。

应用此函数建立与当前环境兼容的日期字符串。


<?php
// set locale to France (on Windows)
setlocale(LC_TIME, "fra_fra");

// format month/day names
// as per locale setting
// returns "septembre" and "mardi"

echo strftime("Month: %B ");
echo strftime("Day: %A ");
?>

 

 


<?php
// format current date into GMT
// returns "13-Sep-2005 08:32 AM"
echo gmdate("d-M-Y h:i A", mktime());
?>

date_default_timezone_set($tz)、date_default_timezone_get()

此函数此后所有的日期/时间函数调用设定并恢复默认的时区。

注:此函数仅在PHP 5.1+中有效。

此函数是一个方便的捷径,可为以后的时间操作设定时区。
microtime()

如前面的setlocale()函数定义的那样,此函数将UNIX时间标签格式化成适用于当前环境的日期字符串。

应用此函数建立与当前环境兼容的日期字符串。


<?php
// get starting value
$start = microtime();

// run some code
for ($x=0; $x<1000; $x++) {
$null = $x * $x;
}

// get ending value
$end = microtime();

// calculate time taken for code execution
echo "Elapsed time: " . ($end - $start) ." sec";
?>

gmmktime($hour, $minute, $second, $month, $day, $year)

此函数由一系列用GMT时间表示的日期与时间值生成一个UNIX时间标签。不用自变量时,它生成一个当前GMT即时时间的UNIX时间标签。

用此函数来获得GMT即时时间的UNIX时间标签。

<?php
// set timezone to UTC
date_default_timezone_set('UTC');
?>

checkdate($month,$date,$year)

如果应用的值构成一个有效日期,则该函数返回为真。例如,对于错误日期2005年2月31日,此函数返回为假。

在日期用于计算或保存在数据库中之前,可用此函数检查日期并使日期生效。

<?php
// returns false
echo checkdate(2,30,2005) ? "valid" : "invalid";
// returns true
echo checkdate(4,6,2010) ? "valid" : "invalid";
?>

getdate($ts)

在没有自变量的情况下,该函数以结合数组的方式返回当前日期与时间。数组中的每个元素代表日期/时间值中的一个特定组成部分。可向函数提交可选的时间标签自变量,以获得与时间标签对应的日期/时间值。

应用此函数来获得一系列离散的,容易分离的日期/时间值。

php 超级强大的数据库连接类

function connect($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $halt = TRUE, $dbcharset2 = '') {

  $func = empty($pconnect) ? 'mysql_connect' : 'mysql_pconnect';
  if(!$this->link = @$func($dbhost, $dbuser, $dbpw, 1)) {
   $halt && $this->halt('Can not connect to MySQL server');
  } else {
   if($this->version() > '4.1') {
    global $charset, $dbcharset;
    $dbcharset = $dbcharset2 ? $dbcharset2 : $dbcharset;
    $dbcharset = !$dbcharset && in_array(strtolower($charset), array('gbk', 'big5', 'utf-8')) ? str_replace('-', '', $charset) : $dbcharset;
    $serverset = $dbcharset ? 'character_set_connection='.$dbcharset.', character_set_results='.$dbcharset.', character_set_client=binary' : '';
    $serverset .= $this->version() > '5.0.1' ? ((empty($serverset) ? '' : ',').'sql_mode=\'\'') : '';
    $serverset && mysql_query("SET $serverset", $this->link);
   }
   $dbname && @mysql_select_db($dbname, $this->link);
  }

 }

 function select_db($dbname) {
  return mysql_select_db($dbname, $this->link);
 }

 function fetch_array($query, $result_type = MYSQL_ASSOC) {
  return mysql_fetch_array($query, $result_type);
 }

 function fetch_first($sql) {
  return $this->fetch_array($this->query($sql));
 }

 function result_first($sql) {
  return $this->result($this->query($sql), 0);
 }

 function query($sql, $type = '') {

  global $debug, $discuz_starttime, $sqldebug, $sqlspenttimes;

  if(defined('SYS_DEBUG') && SYS_DEBUG) {
   @include_once DISCUZ_ROOT.'./include/debug.func.php';
   sqldebug();
  }

  $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
   'mysql_unbuffered_query' : 'mysql_query';
  if(!($query = $func($sql, $this->link))) {
   if(in_array($this->errno(), array(2006, 2013)) && substr($type, 0, 5) != 'RETRY') {
    $this->close();
    require DISCUZ_ROOT.'./config.inc.php';
    $this->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, true, $dbcharset);
    $this->query($sql, 'RETRY'.$type);
   } elseif($type != 'SILENT' && substr($type, 5) != 'SILENT') {
    $this->halt('MySQL Query Error', $sql);
   }
  }

  $this->querynum++;
  return $query;
 }

 function affected_rows() {
  return mysql_affected_rows($this->link);
 }

 function error() {
  return (($this->link) ? mysql_error($this->link) : mysql_error());
 }

 function errno() {
  return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
 }

 function result($query, $row = 0) {
  $query = @mysql_result($query, $row);
  return $query;
 }

 function num_rows($query) {
  $query = mysql_num_rows($query);
  return $query;
 }

 function num_fields($query) {
  return mysql_num_fields($query);
 }

 function free_result($query) {
  return mysql_free_result($query);
 }

 function insert_id() {
  return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
 }

 function fetch_row($query) {
  $query = mysql_fetch_row($query);
  return $query;
 }

 function fetch_fields($query) {
  return mysql_fetch_field($query);
 }

 function version() {
  if(empty($this->version)) {
   $this->version = mysql_get_server_info($this->link);
  }
  return $this->version;
 }

 function close() {
  return mysql_close($this->link);
 }

php 杜绝Cannot add header information

warning: Cannot add header information - headers already sent in

output_buffering被启用时,在脚本发送输出时,PHP并不发送HTTP header。相反,它将此输出通过管道(pipe)输入到动态增加的缓存中(只能在PHP 4。0中使用,它具有中央化的输出机制)。你仍然可以修改/添加header,或者设置cookie,因为header实际上并没有发送。当全部脚本终止时,PHP将自动发送HTTP header到浏览器,然后再发送输出缓冲中的内容。

首先:这错误是怎么产生的呢?让我们来看看PHP是如何处理HTTP header输出和主体输出的。

PHP脚本开始执行时,它可以同时发送header(标题)信息和主体信息。 Header信息(来自 header() 或 SetCookie() 函数)并不会立即发送,相反,它被保存到一个列表中。 这样就可以允许你修改标题信息,包括缺省的标题(例如 Content-Type 标题)。但是,一旦脚本发送了任何非标题的输出(例如,使用 HTML 或 print() 调用),那么PHP就必须先发送完所有的Header,然后终止 HTTP header。而后继续发送主体数据。从这时开始,任何添加或修改Header信息的试图都是不允许的,并会发送上述的错误消息之一。

好!那我们来解决它:

笨方法:把错误警告全不显示!
掩耳盗铃之计,具体方法就不说了 ^_^#

解决方案:

1)适用于有权限编辑PHP。INI的人

打开php。ini文件(你应试比我清楚你的php。ini在哪里),找到

output_buffering =改为on或者任何数字。如果是IIS6,请一定改为ON,不然你的PHP效率会奇慢。

2)使用虚拟主机,不能编辑PHP。INI,怎么办?

简单:

在你的空间根目录下建立一个。htaccess文件,内容如下:

AllowOverride All
PHP_FLAG output_buffering On

不幸的情况是:还是不行?全部网页都不能显示啦?

那么,你可以打电话骂一通空间商,然后让他给你把apache的。htaccess AllowOverride打开

3)在PHP文件里解决

ob_start()
启用output buffering机制。 Output buffering支持多层次 -- 例如,可以多次调用 ob_start() 函数。

ob_end_flush()
发送output buffer(输出缓冲)并禁用output buffering机制。

ob_end_clean()
清除output buffer但不发送,并禁用output buffering。

ob_get_contents()
将当前的output buffer返回成一个字符串。允许你处理脚本发出的任何输出。

 

php图片上传并生成水印

<?php  
header('Content-Type:text/html;charset=gb2312');
$animation = new Imagick();               
$animation->setFormat( "gif" ); 
$image = new Imagick('/skin/'.$_GET["ys"].'.gif');
$unitl = $image->getNumberImages();     
for ($i=0; $i<$unitl; $i++) {
$image->setImageIndex($i);
    $first = new Imagick($_GET["yh"]);
 $bl = new Imagick("/skin/bl.jpg");
    $thisimage = new Imagick();
    $thisimage->readImageBlob($image);
$srcwh = $thisimage->getImageGeometry();
$bl->thumbnailImage ($srcwh["width"],$srcwh["height"],false);
 if ($_GET["kuan"]==0 and $_GET["gao"]==0)
 {
 $first->thumbnailImage ($srcwh["width"],$srcwh["height"],false);     //这里false就是拉伸了 true为不拉伸
 }
 else
 {
 $first->thumbnailImage ($_GET["kuan"],$_GET["gao"],false); //这里false就是拉伸了 true为不拉伸
 }
 $bl->compositeImage($first,Imagick::COMPOSITE_OVER,$_GET["x"],$_GET["y"]);

    $delay = $thisimage->getImageDelay();
    $bl->compositeImage($thisimage,Imagick::COMPOSITE_OVER,0,0);
 $animation->addImage($bl);
    $animation->setImageDelay( $delay ); 
}
$picname=date("ymdhs")."_".mt_rand(10000,99999).".gif";
$name    = "/user/".$picname;
$animation->writeImages($name,1);
echo "<H1></H1>
<DIV class=stborder>
<TABLE cellSpacing=0 cellPadding=0 border=0>
  <TBODY>
  <TR>
    <TD id=picMain align=middle> <img id="qqq" name="qqq" src="/user/{$picname}"><br><iframe   name=demo   style="display:none"></iframe> 
    <a href=#pic onclick="savepic(document.getElementById('qqq'))"><font style="color: #FFFFFF; text-decoration: underline; font-size: 12px;">下载图片</font></a>  <a href=#pic onclick="copypic(0,'qqq')"><font style="color: #FFFFFF; text-decoration: underline; font-size: 12px;">复制图片</font></a>
</TD></TR></TBODY></TABLE></DIV>
<DIV class=clear></DIV>";
//header( "Content-Type: image/gif" );
//echo $animation->getImagesBlob();
//echo $animation;
?>

如果一个包含二元运算符的表达式出现在三元运算符\" ? : \"的\"?\"之前,那么应该给表达式添上一对圆括号。例如: (x >= 0) ? x : -x;

在php很多编程中都会看到?与:的组合用法,这种组合叫做三元运算符了,

运算符"?"前的表达式

如果一个包含二元运算符的表达式出现在三元运算符" ? : "的"?"之前,那么应该给表达式添上一对圆括号。例如:
(x >= 0) ? x : -x;

下面我们来看一个简单的?号表达式实例

<?

$a=1;

echo $a?'true':'false';

输出结果为 trur;

再看

if( $a )

{

   echo 'true';

}

else

{

   echo 'false';

}

用if输出的结果同样也为true哦,从这里可以看了?表达式可以处理一些简单的条件运算。

本站原创转载注明来自:www.111cn.net

[!--infotagslink--]

相关文章

  • php正确禁用eval函数与误区介绍

    eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
  • php中eval()函数操作数组的方法

    在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
  • Python astype(np.float)函数使用方法解析

    这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08
  • Python中的imread()函数用法说明

    这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • 金额阿拉伯数字转换为中文的自定义函数

    CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
  • Android开发中findViewById()函数用法与简化

    findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • PHP用strstr()函数阻止垃圾评论(通过判断a标记)

    strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04
  • 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函数分享之curl方式取得数据、模拟登陆、POST数据

    废话不多说直接上代码复制代码 代码如下:/********************** curl 系列 ***********************///直接通过curl方式取得数据(包含POST、HEADER等)/* * $url: 如果非数组,则为http;如是数组,则为https * $header:...2014-06-07
  • php中的foreach函数的2种用法

    Foreach 函数(PHP4/PHP5)foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。...2013-09-28
  • C语言中free函数的使用详解

    free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25
  • php计算两个日期相差天数的方法

    本文实例讲述了php计算两个日期相差天数的方法。...2015-03-15
  • PHP函数strip_tags的一个bug浅析

    PHP 函数 strip_tags 提供了从字符串中去除 HTML 和 PHP 标记的功能,该函数尝试返回给定的字符串 str 去除空字符、HTML 和 PHP 标记后的结果。由于 strip_tags() 无法实际验证 HTML,不完整或者破损标签将导致更多的数...2014-05-31
  • SQL Server中row_number函数的常见用法示例详解

    这篇文章主要给大家介绍了关于SQL Server中row_number函数的常见用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-08
  • PostgreSQL 字符串处理与日期处理操作

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

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

    分享一个PHP加密解密的函数,此函数实现了对部分变量值的加密的功能。 加密代码如下: /* *功能:对字符串进行加密处理 *参数一:需要加密的内容 *参数二:密钥 */ function passport_encrypt($str,$key){ //加密函数 srand(...2015-10-30