1 second = 1000 millisecond = 1000,000 microsecond = 1000,000,000 nanosecond php的毫秒是没有默认函数的,但提供了一个microtime()函数,该函数返回包含两个元素,一个是秒数,一个是小数表示的毫秒数,借助此函数,可以很容易定义一个返回毫秒数的函数,例如:
1 2 3 4 5 6 7 8 9 10 |
/* * 获取时间差,毫秒级 */ function get_subtraction() { $t1 = microtime(true); $t2 = microtime(true); return (($t2-$1)*1000).'ms'; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
/* * microsecond 微秒 millisecond 毫秒 *返回时间戳的毫秒数部分 */ function get_millisecond() { list($usec, $sec) = explode(" ", microtime()); $msec=round($usec*1000); return $msec; } /* * *返回字符串的毫秒数时间戳 */ function get_total_millisecond() { $time = explode (" ", microtime () ); $time = $time [1] . ($time [0] * 1000); $time2 = explode ( ".", $time ); $time = $time2 [0]; return $time; } /* * *返回当前 Unix 时间戳和微秒数(用秒的小数表示)浮点数表示,常用来计算代码段执行时间 */ function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } |
1 2 3 |
$millisecond = get_millisecond(); $millisecond = str_pad($millisecond,3,'0',STR_PAD_RIGHT); echo date("YmdHis").$millisecond; |
需要注意,在32位系统中php的int最大值远远小于毫秒数,所以不能使用int类型,而php中没有long类型,所以只好使用浮点数来表示。 转载:http://crystalwiner.iteye.com/blog/1567514
View Details定义和用法 rand() 函数返回随机整数。 语法
1 |
rand(min,max) |
参数 描述 min,max 可选。规定随机数产生的范围。 说明 如果没有提供可选参数 min 和 max,rand() 返回 0 到 RAND_MAX 之间的伪随机整数。例如,想要 5 到 15(包括 5 和 15)之间的随机数,用 rand(5, 15)。 提示和注释 注释:在某些平台下(例如 Windows)RAND_MAX 只有 32768。如果需要的范围大于 32768,那么指定 min 和 max 参数就可以生成大于 RAND_MAX 的数了,或者考虑用 mt_rand() 来替代它。 注释:自 PHP 4.2.0 起,不再需要用 srand() 或 mt_srand() 函数给随机数发生器播种,现在已自动完成。 注释:在 3.0.7 之前的版本中,max 的含义是 range 。要在这些版本中得到和上例相同 5 到 15 的随机数,简短的例子是 rand (5, 11)。 例子 本例会返回一些随机数:
1 2 3 4 5 |
<?php echo(rand(); echo(rand(); echo(rand(10,100)) ?> |
输出:
1 2 3 |
17757 3794 97 |
from:http://www.w3school.com.cn/php/func_math_rand.asp
View Detailsfloor函数和ceil函数互相搭配起来可以使php 处理的数据更加真实可靠。 一、先来看floor函数: 语法: float floor ( float value ) 说明: 返回不大于 value 的下一个整数,将 value 的小数部分舍去取整。floor() 返回的类型仍然是 float,因为 float 值的范围通常比 integer 要大。 floor() 例子 1 1 2 3 4 <?php echo floor(1.6); // will output "1" echo floor(-1.6); // will output "-2" ?> floor() 例子 2 1 2 3 4 5 6 7 8 <?php echo(floor(0.60)); echo(floor(0.40)); echo(floor(5)); echo(floor(5.1)); echo(floor(-5.1)); echo(floor(-5.9)) ?> 输出: 0 0 5 5 -6 -6 二、ceil函数: 语法: float ceil ( float value ) 说明: 返回不小于 value 的下一个整数,value 如果有小数部分则进一位。ceil() 返回的类型仍然是 float,因为 float 值的范围通常比 integer 要大。 […]
View DetailsUUID是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的。通常平台 会提供生成UUID的API。UUID按照开放软件基金会(OSF)制定的标准计算,用到了以太网卡地址、纳秒级时间、芯片ID码和许多可能的数字。由以 下几部分的组合:当前日期和时间(UUID的第一个部分与时间有关,如果你在生成一个UUID之后,过几秒又生成一个UUID,则第一个部分不同,其余相 同),时钟序列,全局唯一的IEEE机器识别号(如果有网卡,从网卡获得,没有网卡以其他方式获得),UUID的唯一缺陷在于生成的结果串会比较长。关于 UUID这个标准使用最普遍的是微软的GUID(Globals Unique Identifiers)。 在ColdFusion中可以用CreateUUID()函数很简单的生成UUID,其格式为:xxxxxxxx-xxxx-xxxx- xxxxxxxxxxxxxxxx(8-4-4-16),其中每个 x 是 0-9 或 a-f 范围内的一个十六进制的数字。而标准的UUID格式为:xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx (8-4-4-4-12) <?php function guid(){ if (function_exists('com_create_guid')){ return com_create_guid(); }else{ mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up. $charid = strtoupper(md5(uniqid(rand(), true))); $hyphen = chr(45);// "-" $uuid = chr(123)// "{" .substr($charid, 0, 8).$hyphen .substr($charid, 8, 4).$hyphen .substr($charid,12, 4).$hyphen .substr($charid,16, 4).$hyphen .substr($charid,20,12) .chr(125);// "}" return $uuid; } } echo guid(); ?> from:https://blog.csdn.net/qq3559727/article/details/51752478
View Details//获取浏览器 function getBrowse() { global $_SERVER; $Agent = $_SERVER['HTTP_USER_AGENT']; $browseinfo="; if(ereg('Mozilla', $Agent) && !ereg('MSIE', $Agent)){ $browseinfo = 'Netscape Navigator'; } if(ereg('Opera', $Agent)) { $browseinfo = 'Opera'; } if(ereg('Mozilla', $Agent) && ereg('MSIE', $Agent)){ $browseinfo = 'Internet Explorer'; } if(ereg('Chrome', $Agent)){ $browseinfo="Chrome"; } if(ereg('Safari', $Agent)){ $browseinfo="Safari"; } if(ereg('Firefox', $Agent)){ $browseinfo="Firefox"; } return $browseinfo; } //获取ip function getIP () { global $_SERVER; if (getenv('HTTP_CLIENT_IP')) { $ip = getenv('HTTP_CLIENT_IP'); } else if (getenv('HTTP_X_FORWARDED_FOR')) { $ip = getenv('HTTP_X_FORWARDED_FOR'); } else if (getenv('REMOTE_ADDR')) { $ip = getenv('REMOTE_ADDR'); } else { $ip = $_SERVER['REMOTE_ADDR']; } […]
View Detailscookie 常用于识别用户。 什么是 Cookie? cookie 常用于识别用户。cookie 是服务器留在用户计算机中的小文件。每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie。通过 PHP,您能够创建并取回 cookie 的值。 如何创建 cookie? setcookie() 函数用于设置 cookie。 注释:setcookie() 函数必须位于 <html> 标签之前。 语法
1 |
setcookie(name, value, expire, path, domain); |
例子 在下面的例子中,我们将创建名为 "user" 的 cookie,把为它赋值 "Alex Porter"。我们也规定了此 cookie 在一小时后过期:
1 2 3 4 5 6 7 8 9 |
<?php setcookie("user", "Alex Porter", time()+3600); ?> <html> <body> </body> </html> |
注释:在发送 cookie 时,cookie 的值会自动进行 URL 编码,在取回时进行自动解码(为防止 URL 编码,请使用 setrawcookie() 取而代之)。 如何取回 Cookie 的值? PHP 的 $_COOKIE 变量用于取回 cookie 的值。 在下面的例子中,我们取回了名为 "user" 的 cookie 的值,并把它显示在了页面上:
1 2 3 4 5 6 7 |
<?php // Print a cookie echo $_COOKIE["user"]; // A way to view all cookies print_r($_COOKIE); ?> |
在下面的例子中,我们使用 isset() 函数来确认是否已设置了 cookie:
1 2 3 4 5 6 7 8 9 10 11 12 |
<html> <body> <?php if (isset($_COOKIE["user"])) echo "Welcome " . $_COOKIE["user"] . "!<br />"; else echo "Welcome guest!<br />"; ?> </body> </html> |
如何删除 cookie? 当删除 cookie 时,您应当使过期日期变更为过去的时间点。 删除的例子:
1 2 3 4 |
<?php // set the expiration date to one hour ago setcookie("user", "", time()-3600); ?> |
如果浏览器不支持 cookie 该怎么办? 如果您的应用程序涉及不支持 cookie 的浏览器,您就不得不采取其他方法在应用程序中从一张页面向另一张页面传递信息。一种方式是从表单传递数据(有关表单和用户输入的内容,稍早前我们已经在本教程中介绍过了)。 下面的表单在用户单击提交按钮时向 "welcome.php" 提交了用户输入:
1 2 3 4 5 6 7 8 9 10 11 |
<html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html> |
取回 "welcome.php" 中的值,就像这样:
1 2 3 4 5 6 7 8 |
<html> <body> Welcome <?php echo $_POST["name"]; ?>.<br /> You are <?php echo $_POST["age"]; ?> years old. </body> </html> |
from:http://www.w3school.com.cn/php/php_cookies.asp
View Details
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
/** * 获取当前页面完整URL地址 * * @author 52php.cnblogs.com */ function http_get_page_url() { global $_G; if (empty($_G['pageUrl'])) { $protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://'; $phpSelf = $_SERVER['PHP_SELF'] ? string_safe_replace($_SERVER['PHP_SELF']) : string_safe_replace($_SERVER['SCRIPT_NAME']); $pathInfo = isset($_SERVER['PATH_INFO']) ? string_safe_replace($_SERVER['PATH_INFO']) : ''; $relateUrl = isset($_SERVER['REQUEST_URI']) ? string_safe_replace($_SERVER['REQUEST_URI']) : $phpSelf . (isset($_SERVER['QUERY_STRING']) ? '?' . string_safe_replace($_SERVER['QUERY_STRING']) : $pathInfo); $_G['pageUrl'] = trim($protocal . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '') . $relateUrl, '/'); } // 删除 backUrl 参数 $_G['pageUrl'] = preg_replace('/&backUrl=[^&]+/', '', $_G['pageUrl']); return $_G['pageUrl']; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
/** * 安全过滤函数 * * @param string $string * @return string */ function string_safe_replace($string) { $string = str_replace('%20', '', $string); $string = str_replace('%27', '', $string); $string = str_replace('%2527', '', $string); $string = str_replace('*', '', $string); $string = str_replace('"', '"', $string); $string = str_replace("'", '', $string); $string = str_replace('"', '', $string); $string = str_replace(';', '', $string); $string = str_replace('<', '<', $string); $string = str_replace('>', '>', $string); $string = str_replace("{", '', $string); $string = str_replace('}', '', $string); $string = str_replace('\\', '', $string); return $string; } |
from:https://www.cnblogs.com/52php/p/5669711.html
View Details本文实例讲述了php获取当前url地址的方法。分享给大家供大家参考,具体如下: js 获取: 1 2 top.location.href //顶级窗口的地址 this.location.href //当前窗口的地址 php获取当前url地址: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #测试网址: http://localhost/blog/testurl.php?id=5 //获取域名或主机地址 echo$_SERVER['HTTP_HOST']."<br>"; #localhost //获取网页地址 echo$_SERVER['PHP_SELF']."<br>"; #/blog/testurl.php //获取网址参数 echo$_SERVER["QUERY_STRING"]."<br>"; #id=5 //获取用户代理 echo$_SERVER['HTTP_REFERER']."<br>"; //获取完整的url echo’http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; echo’http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']; #http://localhost/blog/testurl.php?id=5 //包含端口号的完整url echo’http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; #http://localhost:80/blog/testurl.php?id=5 //只取路径 $url=’http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]; echodirname($url); #http://localhost/blog from:http://www.jb51.net/article/102577.htm
View Details