例1
直接把日期转换
代码如下 | 复制代码 |
function daysbetweendates($date1, $date2){ |
例2
代码如下 | 复制代码 |
<?php ?> |
例3
PHP实现两个日期间隔的年、月、周、日数的计算
代码如下 | 复制代码 |
<?php function format($a,$b){ //检查两个日期大小,默认前小后大,如果前大后小则交换位置以保证前小后大 if(strtotime($a)>strtotime($b)) list($a,$b)=array($b,$a); $start = strtotime($a); $stop = strtotime($b); $extend = ($stop-$start)/86400; $result['extends'] = $extend; if($extend<7){ //如果小于7天直接返回天数 $result['daily'] = $extend; }elseif($extend<=31){ //小于28天则返回周数,由于闰年2月满足了 if($stop==strtotime($a.'+1 month')){ $result['monthly'] = 1; }else{ $w = floor($extend/7); $d = ($stop-strtotime($a.'+'.$w.' week'))/86400; $result['weekly'] = $w; $result['daily'] = $d; } }else{ $y= floor($extend/365); if($y>=1){ //如果超过一年 $start = strtotime($a.'+'.$y.'year'); $a = date('Y-m-d',$start); //判断是否真的已经有了一年了,如果没有的话就开减 if($start>$stop){ $a = date('Y-m-d',strtotime($a.'-1 month')); $m =11; $y--; } $extend = ($stop-strtotime($a))/86400; } if(isset($m)){ $w = floor($extend/7); $d = $extend-$w*7; }else{ $m = isset($m)?$m:round($extend/30); $stop>=strtotime($a.'+'.$m.'month')?$m:$m--; if($stop>=strtotime($a.'+'.$m.'month')){ $d=$w=($stop-strtotime($a.'+'.$m.'month'))/86400; $w = floor($w/7); $d = $d-$w*7; } } $result['yearly'] = $y; $result['monthly'] = $m; $result['weekly'] = $w; $result['daily'] = isset($d)?$d:null; } return array_filter($result); } print_r(format('2012-10-1','2012-12-15')); ?> |
常量检测使用defined,定义常量则是define。注意待检测的常量需要使用引号(单双均可),如:
代码如下 | 复制代码 |
if (defined('CONST_NAME')) { |
变量检测则是使用isset,注意变量未声明或声明时赋值为NULL,isset均返回FALSE,如:
代码如下 | 复制代码 |
if (isset($var_name)) { 函数检测用function_exists,注意待检测的函数名也需要使用引号,如: if (function_exists('fun_name')) { |
先不说多了我们看一个实例
代码如下 | 复制代码 |
|
function_exists判断函数是否存在
代码如下 | 复制代码 |
<?php if (function_exists('test_func')) { echo "函数test_func存在"; } else { echo "函数test_func不存在"; } ?> |
filter_has_var函数
filter_has_var() 函数检查是否存在指定输入类型的变量。
若成功,则返回 true,否则返回 false。
代码如下 | 复制代码 |
<?php if(!filter_has_var(INPUT_GET, "name")) { echo("Input type does not exist"); } else { echo("Input type exists"); } ?> |
输出为. Input type exists
本文章来介绍根据用户出生年月来计算年龄/生肖/星座的各种程序实例代码,各位朋友不防进入参考
//计算年龄
代码如下 | 复制代码 |
function birthday($mydate){ $birth=$mydate; list($by,$bm,$bd)=explode('-',$birth); $cm=date('n'); $cd=date('j'); $age=date('Y')-$by-1; if ($cm>$bm || $cm==$bm && $cd>$bd) $age++; return $age; //echo "生日:$birthn年龄:$agen"; } |
根据年份计算生肖
代码如下 | 复制代码 |
<?php echo get_animal(1990); // 马 |
根据生日计算星座
代码如下 | 复制代码 |
echo get_constellation(12, 11); // 射手座 |
取得Response Headers,之后慢慢分析就行了。
给出具体实现方法:
代码如下 | 复制代码 |
$header = get_headers($url, 1); |
实例二
代码如下 | 复制代码 |
<?php $url_parts = @parse_url($url); $sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80), $errno, $errstr, 30); $request = "HEAD " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " HTTP/1.1rn"; if (preg_match('/^Location: (.+?)$/m', $response, $matches)){ } else { } |
【转换原理】:假设IP为:w.x.y.z,则IP地址转为整型数字的计算公式为:intIP = 256*256*256*w + 256*256*x + 256*y + z
【PHP的互转】:PHP的转换方式比较简单,它内置了两个函数
int ip2long ( string $ip_address ) //ip转换成整型数值
string long2ip ( string $proper_address ) // 整型数值转换成ip【MySQL的互转】:相对于MsSQL来说MySQL的转换方式比较简单,它和PHP一样也内置了两个函数
IP 转为整型:
select INET_ATON (IP地址)整型数值转换成IP
select INET_NTOA ( IP的整型数值 )
一个实例
1.手工自己的实现方法
代码如下 | 复制代码 |
$t = explode('.', $ip); { } } function number2ip($num) { for ($i = 0; $i < 4; $i++) |