欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

php 一些经常用到的方法

程序员文章站 2023-12-22 14:24:10
...

获取当前url地址

/**
 * 获取当前url
 * @author Red
 * @date
 * @return string
 */
function getRequestURL() {
    if (!isset($_SERVER['HTTPS']) ||
        $_SERVER['HTTPS'] == 'off' ||
        $_SERVER['HTTPS'] == ''
    ) {
        $scheme = 'http';
    } else {
        $scheme = 'https';
    }
    $host = $_SERVER['SERVER_NAME'];
    $port = $_SERVER['SERVER_PORT'];
    $path = $_SERVER['SCRIPT_NAME'];
    $url  = $_SERVER['REQUEST_URI'];

    return $scheme . '://' . $host . (($port == 80) ? '' : ':' . $port) . $url;
}

 对象转换成数组 

/**
 * 对象转换成数组
 * @author Red
 * @date 2016年1月15日15:32:07
 * @param $obj
 * @return mixed
 */
function object_to_array($obj) {
    $_arr = is_object($obj) ? get_object_vars($obj) :$obj;
    foreach ($_arr as $key=>$val){
        $val = (is_array($val) || is_object($val)) ?object_to_array($val):$val;
        $arr[$key] = $val;
    }
    return $arr;
}

  curl获取文件内容

/**
 * CURL 获取文件内容
 *
 * 用法同 file_get_contents
 *
 * @param string
 * @param integerr
 * @return string
 */
function curl_get_contents($url, $timeout = 10)
{
	if (!function_exists('curl_init'))
	{
		throw new Zend_Exception('CURL not support');
	}

	$curl = curl_init();

	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
	curl_setopt($curl, CURLOPT_HEADER, FALSE);
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
	
	if (defined('WECENTER_CURL_USERAGENT'))
	{
		curl_setopt($curl, CURLOPT_USERAGENT, WECENTER_CURL_USERAGENT);
	}
	else
	{
		curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12');
	}
	
	if (substr($url, 0, 8) == 'https://')
	{
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

		curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
	}

	$result = curl_exec($curl);

	curl_close($curl);

	return $result;
}

  处理2038时间问题

$date = '2090-10-01';
$datetime = new DateTime($date);
echo $datetime->format('Y-m-d H:i');

  16进制转字符串

function hex2str($hex){
    $str = '';
    $arr = str_split($hex, 2);
    foreach($arr as $bit){
        $str .= chr(hexdec($bit));
    }
    return $str;
}
字符串转16进制
function str2hex($str){
    $hex = '';
    for($i=0,$length=mb_strlen($str); $i<$length; $i++){
        $hex .= dechex(ord($str{$i}));
    }
    return $hex;
}
根据身份证号码算出年龄
function get_age_by_id_card($idCard)
{
    $date  = strtotime(substr($idCard, 6, 8));//获得出生年月日的时间戳
    $today = strtotime('today');//获得今日的时间戳
    $diff  = floor(($today - $date) / 86400 / 365);//得到两个日期相差的大体年数
    //strtotime加上这个年数后得到那日的时间戳后与今日的时间戳相比
    return strtotime(substr($idCard, 6, 8) . ' +' . $diff . 'years') > $today ? ($diff + 1) : $diff;
}
function get_deadline($endTime, $starTime = 0)
{
//计算天数
$timeDiff = $endTime - ($starTime ? $starTime : time());
$days = intval($timeDiff / 86400);
//计算小时数
$remain = $timeDiff % 86400;
$hours = intval($remain / 3600);
//计算分钟数
$remain = $remain % 3600;
$mins = intval($remain / 60);

return $days . "天" . $hours . "小时" . $mins . "分钟";
}
 
时间距离

上一篇:

下一篇: