PHP常用函数方法
转自:https://www.php.cn/blog/detail/25535.html
1. countdown 时间格式处理
/** * Notes: [countdown 时间格式处理] * Author HeZe * Date 2020/12/23 18:42 * @param $unix_timestamp * @return array */ function CountDownFormat($unix_timestamp, $format = false) { if ($format) { $date = $unix_timestamp; } else { $date = $unix_timestamp-time(); } $day = $date/60/60/24; $days = (int)$day; $hour = $date/60/60 - $days*24; $hours = (int)$hour; $minute = $date/60 - $days*24*60 - $hours*60; $minutes = (int)$minute; $second = $date - $days*24*60*60 - $hours*60*60 - $minutes*60; $seconds = (int)$second; $result = array( 'days' => $days, 'hours' => $hours, 'minutes' => $minutes, 'seconds' => $seconds, ); return $result; }
2. getDateDiff PHP 时间戳差值文字处理
/** * Notes: [getDateDiff PHP 时间戳差值文字处理] * Author HeZe * Date 2020/12/23 18:38 */ function getDateDiff($dateTimeStamp) { $minute = 60; $hour = $minute * 60; $day = $hour * 24; $halfamonth = $day * 15; $month = $day * 30; $now = time(); // 计算时间差 $diffValue = $now - $dateTimeStamp; if ($diffValue < 0) { return; } $monthC = $diffValue / $month; $weekC = $diffValue / (7 * $day); $dayC = $diffValue / $day; $hourC = $diffValue / $hour; $minC = $diffValue / $minute; $result = ''; if ($monthC >= 1) { $result = intval($monthC)."月前"; } else if ($weekC >= 1) { $result = intval($weekC)."周前"; } else if ($dayC >= 1) { $result = intval($dayC)."天前"; } else if ($hourC >= 1) { $result = intval($hourC)."小时前"; } else if ($minC >= 1) { $result = intval($minC)."分钟前"; } else { $result = "刚刚"; } return $result; }
3. GetWebContentImage 匹配html 中图片路径
/** * Notes: [GetWebContentImage 匹配html 中图片路径] * Author HeZe * Date 2020/12/4 14:16 * @param string $str */ function GetWebContentImage($str = '') { $pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/"; preg_match_all($pattern, $str,$match); return !empty($match['1']) ? $match['1'] : []; }
4. GenerateRandomString获取随机数
/** * Notes: [GenerateRandomString获取随机数] * Author HeZe * Date 2020/12/4 14:16 * @param string $str */ function GenerateRandomString($length = 10, $is_char = false) { $characters = '0123456789'; if ($is_char) { $characters .= 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; } $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, strlen($characters) - 1)]; } return $randomString; }
5. EncryptMobile 手机号码加密显示
/** * Notes: [EncryptMobile 手机号码加密显示] * Author HeZe * Date 2020/12/4 14:16 * @param string $str */ function EncryptMobile ($mobile = '', $points = '*') { if (!empty($mobile)) { $start = substr($mobile, 0, 3); $end = substr($mobile, strlen($mobile)-4, 4); $ReturnData = array( 'mobile' => $start.'****'.$end, ); return DataReturn('获取成功!', 0, $ReturnData); } return DataReturn('参数错误!', -100); }