
ฟังก์ชั่น คำนวณอายุตัวนี้ ผมแปลงมาจากฟังก์ชั่นของ codeigniter เห็นว่าใช้งานได้ดีครับ ผลที่ได้ออกมา จะเป็นจำนวนปี เดือน สัปดาห์ วัน นาที วินาที เรียงไล่ลำดับไป
พารามิเตอร์จะมีสองตัว ตัวแรกให้ส่งวันเกิดเข้าไป ตัวที่สองวันปัจจุบัน โดยพารามิเตอร์ทั้งสองตัว ต้องเป็น timestamp แต่ไม่ต้องกังวลนะครับ ในโค้ดผมได้แสดงตัวอย่างไว้ให้ดูแล้วด้วย ท่านเห็นปุ๊บก็เข้าใจปั๊บ ผมใช้ฟังก์ชั่น strtotime แปลงวันเกิดให้เป็น timestamp
<?php function timespan($seconds = 1, $time = '') { if ( ! is_numeric($seconds)) { $seconds = 1; } if ( ! is_numeric($time)) { $time = time(); } if ($time <= $seconds) { $seconds = 1; } else { $seconds = $time - $seconds; } $str = ''; $years = floor($seconds / 31536000); if ($years > 0) { $str .= $years.' ปี, '; } $seconds -= $years * 31536000; $months = floor($seconds / 2628000); if ($years > 0 OR $months > 0) { if ($months > 0) { $str .= $months.' เดือน, '; } $seconds -= $months * 2628000; } $weeks = floor($seconds / 604800); if ($years > 0 OR $months > 0 OR $weeks > 0) { if ($weeks > 0) { $str .= $weeks.' สัปดาห์, '; } $seconds -= $weeks * 604800; } $days = floor($seconds / 86400); if ($months > 0 OR $weeks > 0 OR $days > 0) { if ($days > 0) { $str .= $days.' วัน, '; } $seconds -= $days * 86400; } $hours = floor($seconds / 3600); if ($days > 0 OR $hours > 0) { if ($hours > 0) { $str .= $hours.' ชั่วโมง, '; } $seconds -= $hours * 3600; } $minutes = floor($seconds / 60); if ($days > 0 OR $hours > 0 OR $minutes > 0) { if ($minutes > 0) { $str .= $minutes.' นาที, '; } $seconds -= $minutes * 60; } if ($str == '') { $str .= $seconds.' วินาที'; } return substr(trim($str), 0, -1); } // ตัวอย่างการใช้งาน $birthdate = strtotime( '1973-11-13' ); $today = time(); echo timespan( $birthdate , $today ); //36 ปี, 2 เดือน, 3 สัปดาห์, 2 วัน, 4 ชั่วโมง, 51 นาที ?>
เห็นมั้ยครับการใช้ PHP คำนวณอายุนั้นไม่ยากเลย
บทความน่าสนใจ
- ความเข้าใจเรื่องฟิลด์ชนิด timestamp ของ mysql
- แนะนำเว็บให้ความรู้การเขียนโปรแกรมโคตะระเด็ด
- PHP date() ฟังก์ชั่นจัดการวัน-เวลา
- แล่ว แล่ว แล่ว แล่ว แล่ว แล้ว…….
Tags: PHP
ผู้เขียน: วัชรเมธน์ ชิษณุคุปต์ ศรีเนธิโรทัย
เว็บไซต์ :http://www.select2web.com
งานเล็กๆก็ยิ่งใหญ่ได้ ถ้าอยู่ในที่ๆเหมาะสม
มี 1 ความเห็น ในบทความเรื่อง “แจกโค้ด php คำนวณอายุ”
แสดงความเห็น
Categories
- 1 week 1 Source (2)
- AppServ (10)
- Array (2)
- Codeigniter (1)
- Computer (7)
- CSS (9)
- Dreamweaver (7)
- Firefox (7)
- Form (2)
- FPDF (15)
- General (31)
- HTML (2)
- Javascript (6)
- jQuery (86)
- Mac (10)
- MySQL (15)
- Navicat (9)
- Other (58)
- PHP (97)
- PHP-function (12)
- Program (8)
- SMF (15)
- SQL (20)
- Theme (1)
- TinyMCE (7)
- webservice (1)
- Wordpress (18)
- คอมฯ ทั่วไป (6)
-
Recent Posts
- akismet ปลักอินกรองแสปมของเวิร์ดเพรส
- WP-CMS Post Control ปลักอินสำหรับซ่อนสิ่งที่ไม่ต้องการในเวิร์ดเพรส
- wordpress flickr manager ปลักอินสำหรับอัปโหลดภาพเก็บที่ flickr
- picpick โปรแกรมที่เว็บโปรแกรมเมอร์ต้องมีไว้ติดเครื่อง
- ขอความเห็น
- todoist โปรแกรมจดรายการสิ่งที่ต้องทำสุดเจ๋ง
- wordpress google xml sitemap หนึ่งในปลักอินที่ท่านต้องติดตั้ง
- แนะนำเว็บให้ความรู้การเขียนโปรแกรมโคตะระเด็ด
- โปรแกรมจดรายการสิ่งที่จะต้องทำ
- แจกภาพปุ่มเอาไปตกแต่งเว็บ
- วิธีสร้างเว็บให้ติดอันดับหนึ่ง
- wordpress ปิงผู้ปิดทองหลังพระ
- วิธีเปิด notepad อย่างรวดเร็ว
- วิธีดึงข้อมูลออกจากฐานข้อมูลของเวิร์ดเพรส แบบสุ่ม
- หารายละเอียดโดเมน
Tags
AppServ Array Back to the basic bug calendar CAPTCHA chart CSS Dreamveawer eclipse error firebug Firefox firefox plugin FPDF General google helios HTML icons Javascript jQuery jquery plugin jquery slide jQuery tutorial jQuery UI Mac MySQL Other pdt PHP phpmailer programming slide slide show SMF smf themes smf themes download smf themes free smf themes thai SQL sql function tooltip Wordpress wordpress plugin
ขอบคุณครับ