รวม PHP Utils ฟังก์ชั่นอรรถประโยชน์ที่ใช้งานบ่อย
รวม PHP Utils ฟังก์ชั่นอรรถประโยชน์ที่ใช้งานบ่อย
สำหรับคนทำเว็บไซต์ แน่นอนว่าจะต้องมีฟังก์ชั่นบางอย่างที่ต้องใช้งานอยู่บ่อย ๆ ซึ่งเรามักจะเรียกว่า Utils function หรือฟังก์ชั่นอรรถประโยชน์ วันนี้ได้รวบรวมฟังก์ชั่นที่จำเป็นสำหรับการพัฒนาเว็บไซต์เก็บไว้ในรูปแบบคลาสสามารถทำไปใช้งานต่อยอดได้เลย ลองมาดูกันครับว่ามีฟังก์ชั่นอะไรบ้าง
ตัวอย่าง PHP Utils ฟังก์ชั่นอรรถประโยชน์
- 1. ฟังก์ชั่นแปลง timestamp เป็นจำนวันเวลาแบบละเอียด
1234567891011121314151617181920212223242526272829303132333435363738394041function time_elapsed($datetime, $full = false) {date_default_timezone_set("Asia/Bangkok");$now = new DateTime;$ago = new DateTime($datetime);$diff = $now->diff($ago);$diff->w = floor($diff->d / 7);$diff->d -= $diff->w * 7;$string = array('y' => 'year','m' => 'month','w' => 'week','d' => 'day','h' => 'hour','i' => 'minute','s' => 'second');foreach ($string as $k => &$v) {if ($diff->$k) {$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');} else {unset($string[$k]);}}if (!$full) $string = array_slice($string, 0, 1);$string = str_replace('months','เดือน',$string);$string = str_replace('month','เดือน',$string);$string = str_replace('weeks','สัปดาห์',$string);$string = str_replace('week','สัปดาห์',$string);$string = str_replace('days','วัน',$string);$string = str_replace('day','วัน',$string);$string = str_replace('hours','ชั่วโมง',$string);$string = str_replace('hour','ชั่วโมง',$string);$string = str_replace('minutes','นาที',$string);$string = str_replace('minute','นาที',$string);$string = str_replace('seconds','วินาที',$string);$string = str_replace('second','วินาที',$string);return $string ? $string = implode(' ', $string) . ' ที่แล้ว' : 'เมื่อสักครู่';}
วิธีใช้งานและผลลัพธ์เมื่อเรียกใช้งาน
12echo 'time_elapsed : '.Utils::time_elapsed($timestamp,true).'<br>';time_elapsed : 8 เดือน 4 สัปดาห์ 22 ชั่วโมง 46 นาที 25 วินาที ที่แล้ว -
2. ฟังก์ชั่นแปลงตัวเลขเป็นตัวย่อ
12345678910111213141516171819function number_shorten($number, $precision = 3, $divisors = null) {if (!isset($divisors)) {$divisors = array(pow(1000, 0) => '',pow(1000, 1) => 'K',pow(1000, 2) => 'M',pow(1000, 3) => 'B',pow(1000, 4) => 'T',pow(1000, 5) => 'Qa',pow(1000, 6) => 'Qi',);}foreach ($divisors as $divisor => $shorthand) {if (abs($number) < ($divisor * 1000)) {break;}}return number_format($number / $divisor, $precision) . $shorthand;}วิธีใช้งานและผลลัพธ์เมื่อเรียกใช้งาน
12echo 'number_shorten : '.Utils::number_shorten($thaibaht,0).'<br>';number_shorten : 34K
ตัวอย่างฟังก์ชั่นอื่น ๆ ใน PHPUtils.php
1 2 3 4 5 6 7 8 9 10 11 |
require_once("PHPUtils.php"); $timestamp = '2016-11-15 14:53:23'; $thaibaht = 34000; echo 'time_elapsed : '.Utils::time_elapsed($timestamp,true).'<br>'; // แปลง timestamp เป็นจำนวนวันเวลาที่ผ่านมา echo 'number_shorten : '.Utils::number_shorten($thaibaht,0).'<br>'; // แปลงตัวเลขเป็นตัวย่อ echo 'random_code : '.Utils::random_code(10).'<br>'; // สุ่มรหัสตัวเลข ตัวอักษร echo 'get_thai_date : '.Utils::get_thai_date($timestamp).'<br>'; // แปลง timestamp เป็นวันเดือนปีไทย echo 'day_diff : '.Utils::day_diff($timestamp).'<br>'; // หาจำนวนวันที่ผ่านมาจาก timestamp echo 'get_client_ip : '.Utils::get_client_ip().'<br>'; // หาไอพีเครื่อง |
สำ Web Developer ที่พัฒนาเว็บไซต์ด้วย codeigniter สามารถนำคลาส PHPUtils.php ไปวางไว้ในส่วนของ libraries จากนั้นสามารถเปิดใช้งานคลาสได้ 2 ช่องทางดังนี้
เรียกใช้งานในส่วนของ controller
1 |
$this->load->library('phputils'); |
เรียกใช้งานผ่าน autoload
1 |
$autoload['libraries'] = array('phputils'); |