Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > บทความจากสมาชิก > PHP dateTimeFormat รูปแบบวันที่ (รูปแบบวันที่ภาษาไทยใน PHP)



 
Clound SSD Virtual Server

PHP dateTimeFormat รูปแบบวันที่ (รูปแบบวันที่ภาษาไทยใน PHP)

DateTimeFormat.php
Code
<?php
/******************************************************************************
 * 
 * Name: DateTimeFormat.class.php
 * Purpose: Formate date time with PHP classes.
 * Author:  Narong Rammanee
 *
 ******************************************************************************
 *
 * Copyright 2011 Narong <[email protected]>
 *      
 * This class is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *      
 * This class is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *      
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 ******************************************************************************/
 
class DateTimeFormat
{
    /**
	 * Day
	 * @var string
	 */
	public $_day;
    
    /**
	 * Month
	 * @var string
	 */
    public $_month;
    
    /**
	 * Year
	 * @var string
	 */
    public $_year;
    
    /**
	 * Hour
	 * @var string
	 */
    public $_hour;
    
    /**
	 * Minute
	 * @var string
	 */
    public $_minute;
    
    /**
	 * Second
	 * @var string
	 */
    public $_second;
    
    /**
	 * DateTime format
	 * @var string
	 */
    public $_format;
    
    /**
	 * DateTime separator
	 * @var string
	 */
    public $_separator;
    
    /**
	 * Language
	 * @var string
	 */
    public $_lang;
    
    /**
	 * Constructor
	 * Initialization that the object may need before it is used.
	 *  
	 * @param $datetime
	 * @param $format
	 * @param $separator
	 * @param $lang
	 * @return void.
	 */
    public function __construct($datetime=null, $format=null, $separator='/', $lang='th') 
	{
		self::setDateTime($datetime, $format, $separator, $lang);
	}
    
    /**
	 * Set DateTime.
	 *  
	 * @param $datetime
	 * @param $format
	 * @param $separator
	 * @param $lang
	 * @return void.
	 */
    public function setDateTime($datetime=null, $format=null, $separator='', $lang='th')
    {
        $pdate = date_parse($datetime);
        
        $this->_day       = $pdate['day'];
        $this->_month     = $pdate['month'];
        $this->_year      = $pdate['year'];
        $this->_hour      = $pdate['hour'];
        $this->_minute    = $pdate['minute'];
        $this->_second    = $pdate['second'];
        $this->_format    = $format;
        $this->_lang      = $lang;
        $this->_separator =  $separator == '' ? $this->_separator : $separator;
    }
    
    /**
	 * Render DateTime.
	 *
	 * @return String DateTime.
	 */
	public function render()
	{        
        $formatArray = explode(" ", $this->_format);
        
        $formatDate = $formatArray[0];
        $formatTime = $formatArray[1];
        
        $formatDateArray = explode("-", $formatDate);
        $date = self::date($formatDateArray, $this->_separator);
        
        $time = '';
        if(!empty($formatTime)) {
            $formatTimeArray = explode(":", $formatTime);
            $time = self::time($formatTimeArray);
        }
        
        $datetime = $date . ' ' . $time;
        
		return $datetime;
	}
    
    /**
	 * Format day.
	 *
	 * @return String Day.
	 */
    private function day($format='')
    {
        $fullday  = array(
            1 => 'จันทร์', 2 => 'อังคาร', 3 => 'พุธ', 4 => 'พฤหัสบดี', 
            5 => 'ศุกร์', 6 => 'เสาร์', 7 => 'อาทิตย์'
        );
        $shortday = array(
            1 => 'จ.', 2 => 'อ.', 3 => 'พ.', 4 => 'พฤ.',
            5 => 'ศ.', 6 => 'ส.', 7 => 'อา.'
        );
        
        switch ($format) {
            case 'd'  : $day = $this->_day; break;
            case 'dd' : $day = self::zeroFill($this->_day); break;
            case 'sd' : $day = $shortday[$this->_day]; break;
            case 'fd' :
                if (self::isThaiLang()) {
                    $d = date("N", mktime(0, 0, 0, $this->_month, $this->_day, $this->_year));
                    $day = 'วัน' . $fullday[$d] . 'ที่ ' . $this->_day;
                } else {
                    $d = date("l", mktime(0, 0, 0, $this->_month, $this->_day, $this->_year));
                    $day = $d . ' of';
                }
                break;
            case 'fD' :
                $day = 'วัน' . $fullday[$d] . 'ที่ ' . self::numeral($this->_day);
                break;
            case 'D'  : $month = self::numeral($this->_day); break;
            case 'DD' : $day = self::numeral(self::zeroFill($this->_day)); break;
            default : $day = self::zeroFill($this->_day);
        }
        
        return $day;
    }
    
    /**
	 * Format month.
	 *
	 * @return String Month.
	 */
    private function month($format='')
    {
        
        $fullmonth = array(
			1 => 'มกราคม', 2 => 'กุมภาพันธ์', 3 => 'มีนาคม', 4 => 'เมษายน',
			5 => 'พฤษภาคม', 6 => 'มิถุนายน', 7 => 'กรกฎาคม', 8 => 'สิงหาคม',
			9 => 'กันยายน', 10 => 'ตุลาคม', 11 => 'พฤศจิกายน', 12 => 'ธันวาคม'
		);
		$shortmonth = array(
			1 => 'ม.ค.', 2 => 'ก.พ.', 3 => 'มี.ค.', 4 => 'เม.ย.', 
			5 => 'พ.ค.', 6 => 'มิ.ย.', 7 => 'ก.ค.', 8 => 'ส.ค.',
			9 => 'ก.ย.', 10 => 'ต.ค.', 11 => 'พ.ย.', 12 => 'ธ.ค.'
		);
        
        switch ($format) {
            case 'm'  : $month = $this->_month; break;
            case 'mm' : $month = self::zeroFill($this->_month); break;
            case 'sm' : 
                if (self::isThaiLang()) {
                    $month = $shortmonth[$this->_month];
                } else {
                    $m = $this->_month + 1;
                    if($m == 13) $m = 1;
                    $month = date("M", mktime(0, 0, 0, $m, 0, 0));
                }
                break;
            case 'fm' :
                if (self::isThaiLang()) {
                    $month = $fullmonth[$this->_month];
                } else {
                    $m = $this->_month + 1;
                    if($m == 13) $m = 1;
                    $month = date("F", mktime(0, 0, 0, $m, 0, 0));
                }
                break;
            case 'fM' : $month = $fullmonth[$this->_month]; break;
            case 'M'  : $month = self::numeral($this->_month); break;
            case 'MM' : $month = self::numeral(self::zeroFill($this->_month)); break;
            default: $month = self::zeroFill($this->_month);
        }
        
        return $month;
    }
    
    /**
	 * Format year.
	 *
	 * @return String Year.
	 */
    private function year($format='')
    {
        $year = $this->_lang == 'en' ? $this->_year : $this->_year + 543;
        
        switch ($format) {
            case 'yy' : $year = substr($year, -2); break;
            case 'YY' : $year = substr($year, -2);
                $year = self::numeral($year);
                break;
            case 'yyyy' : $year; break;
            case 'YYYY' : 
                $year = self::numeral($year);
                break;
            default:
                $year = $this->_year;
        }
        
        return $year;
    }
    
    /**
	 * Format hour.
	 *
	 * @return String Hour.
	 */
    public function hour($format='')
    {
        switch ($format) {
            case 'h'  : $hour = $this->_hour; break;
            case 'hh' : $hour = self::zeroFill($this->_hour); break;
            case 'H'  : $hour = self::numeral($this->_hour); break;
            case 'HH' : $hour = self::numeral(self::zeroFill($this->_hour)); break;
            default: $hour = '';
        }
        
        return $hour;
    }
    
    /**
	 * Format minute.
	 *
	 * @return String Minute.
	 */
    public function minute($format='')
    {
        switch ($format) {
            case 'm'  : $minute = $this->_minute; break;
            case 'mm' : $minute = self::zeroFill($this->_minute); break;
            case 'M'  : $minute = self::numeral($this->_minute); break;
            case 'MM' : $minute = self::numeral(self::zeroFill($this->_minute)); break;
            default: $minute = '';
        }
        
        $minute = $minute != '' ? ':' . $minute : '';
        
        return $minute;
    }
    
    /**
	 * Format second.
	 *
	 * @return String Second.
	 */
    public function second($format='')
    {
        switch ($format) {
            case 's'  : $second = $this->_second; break;
            case 'ss' : $second = self::zeroFill($this->_second); break;
            case 'S'  : $second = self::numeral($this->_second); break;
            case 'SS' : $second = self::numeral(self::zeroFill($this->_second)); break;
            default: $second = '';
        }
        
        $second = $second != '' ? ':' . $second : '';
        
        return $second;
    }
    
    /**
	 * Format Date.
	 *
	 * @return String Date.
	 */
    public function date($format, $separator)
    {
        $date = '';
        $index = 0;
        $lastIndex = count($format) - 1;

        foreach ($format as $key => $value) {
            
            if($value == 'sd' || $value == 'fd' 
                || $value == 'fD' || $value == 'fM')
                $separator = " ";
        
            $tmp = substr($value, -1);
    
            if($tmp == 'd' || $tmp == 'D') {
                $day = self::day($value);
                $date .= $day;
                $date .= ($index != $lastIndex) ? $separator : '';
            }
            else if($tmp == 'm' || $tmp == 'M' ) {
                $month = self::month($value);
                $date .= $month;
                $date .= ($index != $lastIndex) ? $separator : '';
            }
            else {
                $year  = self::year($value);
                $date .= $year;
                $date .= ($index != $lastIndex) ? $separator : '';
            }
            
            $index++;
        }
        
        if($lastIndex == 0) {
            $day   = self::day();
            $month = self::month();
            $year  = self::year();
            
            $date = $year . $separator . $month . $separator . $day;
        }
        
        return $date;
    }
    
    /**
	 * Format time.
	 *
	 * @return String Time.
	 */
    public function time($format=null)
    {
        $end = $this->_lang == 'th' ? ' น.' : '';
        
        $formatHour   = $format[0];
        $formatMinute = $format[1];
        $formatSecond = $format[2];
        
        $hour   = self::hour($formatHour);
        $minute = self::minute($formatMinute);
        $second = self::second($formatSecond);
        
        $time = $hour . $minute .$second . $end;
        
        return $time;
    }
    
    /**
	 * Thai numeral.
	 *
	 * @return String Thai numeral.
	 */
    private static function numeral($n)
	{
        $thNumber = array('๐', '๑', '๒', '๓', '๔', '๕', '๖', '๗', '๘', '๙');
        $arNumber = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
		
        $number = str_replace($arNumber, $thNumber, $n);
        
		return $number;
	}
	
    /**
	 * Zero fill.
	 *
	 * @return String with zero fill.
	 */
	private static function zeroFill($n)
	{
		return $n < 10 ? '0' . $n : $n;
	}
    
    private function isThaiLang()
    {
        $thaiLang = $this->_lang == 'th' ? true : false; 
        return $thaiLang;
    }
}



init.inc.php
<?php
/******************************************************************************
 * 
 * Name: init.inc.php
 * Purpose: Initialize load class.
 * Author:  Narong Rammanee
 *
 ******************************************************************************
 *
 * Copyright 2010 Narong <[email protected]>
 *      
 * This class is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *      
 * This class is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *      
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 ******************************************************************************/

// Define protocol.
define(PROTOCOL, 'http://');

// Define host name.
define(HOST_NAME, $_SERVER['HTTP_HOST']);

// Define project name.
define(PROJ_NAME, '/dateTimeFormat');

// Define base url of project.
define(BASE_URL, PROTOCOL . HOST_NAME . PROJ_NAME);

// Automatically called in case you are trying to use a class.
function __autoload($classname) 
{
	// Class file path.
	$filepath = dirname(__FILE__) . '/classes/' . $classname . '.class.php';
	if(file_exists($filepath)) {
		// Include class file.
		require_once $filepath;
	} else {
		// If can't load class display error message.
		throw new Exception(
			'<pre class="error"><h1>Unable to load:</h1> ' . $filepath . '</pre>'
		);
	}
}



index.php
Code
<?php 
	// Include Initialize file.
	require_once dirname( __FILE__ ) . '/init.inc.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Nautilus's date time format php class</title>
	<link type="text/css" rel="stylesheet" href="<?php echo BASE_URL; ?>/css/default.css"/>
</head>

<body>
    <h1>Example::</h1>
<?php	
	$now = '1984-06-30 13:30';
	
	$date = new DateTimeFormat();
    
	echo '<div id="container">';
		echo '<h1>Nautilus\'s format date time</h1>';
		echo '<pre>';
		$date->setDateTime($now, 'dd-m-yy', '/');
		echo '<p> => ', $date->render(), '</p>';
        $date->setDateTime($now, 'dd-m-yyyy');
        echo '<p> => ', $date->render(), '</p>';
        $date->setDateTime($now, 'dd-m-yyyy hh:mm');
        echo '<p> => ', $date->render(), '</p>';
        $date->setDateTime($now, 'DD-M-YY');
		echo '<p> => ', $date->render(), '</p>';
        $date->setDateTime($now, 'DD-MM-YYYY');
		echo '<p> => ', $date->render(), '</p>';
        $date->setDateTime($now, 'DD-MM-YYYY HH:MM');
		echo '<p> => ', $date->render(), '</p>';
        $date->setDateTime($now, 'dd-sm-yyyy',  ' ');
		echo '<p> => ', $date->render(), '</p>';
        $date->setDateTime($now, 'dd-fm-yyyy');
		echo '<p> => ', $date->render($now, ''), '</p>';
        $date->setDateTime($now, 'DD-sm-YYYY');
		echo '<p> => ', $date->render(), '</p>';
        $date->setDateTime($now, 'DD-fm-YYYY');
		echo '<p> => ', $date->render(), '</p>';
        $date->setDateTime($now, 'fd-fm-yyyy hh:mm');
		echo '<p> => ', $date->render(), '</p>';
        
		echo '</pre>';
	echo '</div>';
?>

</body>
</html>







   
Share
Bookmark.   

  By : ranarong
  Article : บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ
  Score Rating :
  Create Date : 2009-09-18
  Download : No files
Sponsored Links
ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่