今天做项目的时候用到两个实用的php方法,相信大家平时也可能会遇到;

于是我就像把这些方法记录下来方便查阅。

我会不断更新这篇文章,大家也可以收藏起来,方便查询。

1.根据评论时间或消息时间对比输出时间记录

如图:


public static function humandate($timestamp) {
        $time = time();
        $seconds = $time - $timestamp;
        if($seconds > 31536000) {
            return date('Y-n-j', $timestamp);
        } elseif($seconds > 2592000) {
            return floor($seconds / 2592000).'月前';
        } elseif($seconds > 86400) {
            return floor($seconds / 86400).'天前';
        } elseif($seconds > 3600) {
            return floor($seconds / 3600).'小时前';
        } elseif($seconds > 60) {
            return floor($seconds / 60).'分钟前';
        } else {
            return $seconds.'秒前';
        }
    }


2.php 访问接口返回结果或参数

public static function http($url,$data=null) {
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        if(!empty($data)) 
        {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_URL, $url);
        $res = curl_exec($curl);
        curl_close($curl);
        
        return $res;
    } 

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
意见
建议
发表
评论
返回
顶部