12个常用数学函数
函数名 | 描述 | 实例 | 输入 | 输出 |
---|---|---|---|---|
abs() | 求绝对值 | $abs = abs(-4.2); //4.2 | 数字 | 绝对值数字 |
ceil() | 进一法取整 | echo ceil(9.999); // 10 | 浮点数 | 进一取整 |
floor() | 舍去法取整 | echo floor(9.999); // 9 | 浮点数 | 直接舍去小数部分 |
fmod() | 浮点数取余 | $x = 5.7;$y = 1.3; $r = fmod($x, $y); // $r equals 0.5, because 4 * 1.3 + 0.5 = 5.7 | 两个浮点数,x>y | 浮点余数 |
pow() | 返回数的n次方 | echo pow(-1, 20); // 1 | 基础数|n次方 | 乘方值 |
round() | 浮点数四舍五入 | echo round(1.95583, 2);// 1.96 | 一个数值|保留小数点后多少位,默认为0 | 舍入后的结果 |
sqrt() | 求平方根 | echo sqrt(9); //3 | 被开方的数 | 平方根 |
max() | 求最大值 | echo max(1, 3, 5, 6, 7); // 7 echo max(array(2, 4, 5)); // 5 | 多个数字或一个数组 | 返回其中的最大值 |
min() | 求最小值 | 多个数字或数组 | 返回其中的最小值 | |
mt_rand() | 更好的随机数 | echo mt_rand(0,9);//n | 最小|最大,随机数 | 随机返回范围内的值 |
rand() | 随机数 | 最小|最大,随机数 | 随机返回范围内的值 | |
pi() | 获取圆周率值 | echo pi(); // 3.1415926535898 | 无 | 获取圆周率 |
8个常用时间函数
函数名 | 描述 | 实例 | 输入 | 输出 | 操作 |
---|---|---|---|---|---|
time() | 返回当前的 Unix 时间戳 | time(); | int time ( void ) | 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数 | |
mktime() | 取得一个日期的 Unix 时间戳 | mktime(0, 0, 0, 4, 25, 2012); | int mktime ([ int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year [, int $is_dst ]]]]]]] ) | ||
date() | 格式化一个本地时间/日期 | date(‘Y年m月d日 H:i:s’); | string date ( string $format [, int $timestamp ] ) | 2012年04月25日 20:45:54 | |
checkdate() | 验证一个格里高里日期 | if(checkdate(6,31,2012)){ echo ‘成立’; }else{ echo ‘不成立’; } | bool checkdate ( int $month , int $day , int $year ) | 如果给出的日期有效则返回 TRUE,否则返回 FALSE | |
date_default_timezone_set() | 设定用于一个脚本中所有日期时间函数的默认时区 | date_default_timezone_set(‘PRC’); | bool date_default_timezone_set ( string $timezone_identifier ) | ||
getdate() | 取得日期/时间信息 | $t=getdate(); var_dump($t); | array getdate ([ int $timestamp ] ) | 返回一个根据 timestamp 得出的包含有日期信息的结合数组。如果没有给出时间戳则认为是当前本地时间 | |
strtotime() | 将任何英文文本的日期时间描述解析为 Unix 时间戳 | echo strtotime(“now”); echo strtotime(“10 September 2000”); echo strtotime("+1 day"); echo strtotime("+1 week"); echo strtotime("+1 week 2 days 4 hours 2 seconds"); echo strtotime(“next Thursday”); echo strtotime(“last Monday”); | int strtotime ( string $time [, int $now ] ) | ||
microtime() | 返回当前 Unix 时间戳和微秒数 | $start=microtime(true); sleep(3); $stop=microtime(true); echo $stop-$start; | mixed microtime ([ bool $get_as_float ] ) |