<?php
function strtothaidate($date){
	$d=substr($date,0,2);
	$m=substr($date,3,2);
	$y=substr($date,6,4);
	if(checkdate((int)$m,(int)$d,(int)$y)==true){
		$y=(int)$y+543;
		$date=adodb_date("d/m",adodb_mktime(0,0,0,$m,$d,0))."/".$y;
		return $date;
	}else{
		return null;
	};
};

function ytoday(){
	$now = getdate(time());
	$d=$now["mday"];
	$m=$now["mon"];
	$y=$now["year"];
	if(checkdate((int)$m,(int)$d,(int)$y)==true){
		$y=(int)$y;
		$date=$y.adodb_date("md",mktime(0,0,0,$m,$d,0));
		return $date;
	}else{
		return null;
	};
};
function thaitoday(){
	$now = getdate(time());
	$d=$now["mday"];
	$m=$now["mon"];
	$y=$now["year"];
	if(checkdate((int)$m,(int)$d,(int)$y)==true){
		$y=(int)$y+543;
		$date=adodb_date("d/m",mktime(0,0,0,$m,$d,0))."/".$y;
//		$date=adodb_date("d/m",mktime(0,0,0,$m,$d,$y)).$y;
		return $date;
	}else{
		return null;
	};
};

function ythaitoday(){
	$now = getdate(time());
	$d=$now["mday"];
	$m=$now["mon"];
	$y=$now["year"];
	if(checkdate((int)$m,(int)$d,(int)$y)==true){
		$y=(int)$y+543;
		$date=date("d/m",mktime(0,0,0,$m,$d,0))."/".$y;
//		$date=date("d/m",mktime(0,0,0,$m,$d,$y)).$y;
		return $date;
	}else{
		return null;
	};
};

function xthaitoday($v){
	$now = getdate(time()+($v*24*60*60));
	$d=$now["mday"];
	$m=$now["mon"];
	$y=$now["year"];
	if(checkdate((int)$m,(int)$d,(int)$y)==true){
		$y=(int)$y+543;
		$date=adodb_date("d/m",mktime(0,0,0,$m,$d,0))."/".$y;
//		$date=adodb_date("d/m",mktime(0,0,0,$m,$d,$y)).$y;
		return $date;
	}else{
		return null;
	};
};

function thaidbtodate($date){
        if(empty($date)){
	$now = getdate(time());
	$d=$now["mday"];
	$m=$now["mon"];
	$y=$now["year"];
	}else{
	$d=substr($date,8,2);
	$m=substr($date,5,2);
	$y=substr($date,0,4);
	};
	if(checkdate((int)$m,(int)$d,(int)$y)==true){
		$y=(int)$y+543;
		$date=adodb_date("d/m",adodb_mktime(0,0,0,$m,$d,0))."/".$y;
		return $date;
	}else{
		return null;
	};
};

function thaidbtoYMD($date){
        if(empty($date)){
	$now = getdate(time());
	$d=$now["mday"];
	$m=$now["mon"];
	$y=$now["year"];
	}else{
	$d=substr($date,8,2);
	$m=substr($date,5,2);
	$y=substr($date,0,4);
	};
	if(checkdate((int)$m,(int)$d,(int)$y)==true){
		//$y=(int)$y+543;
		$date=$y.adodb_date("md",adodb_mktime(0,0,0,$m,$d,0));
		return $date;
	}else{
		return null;
	};
};

function shortthaidbtodate($date){
	$d=substr($date,8,2);
	$m=substr($date,5,2);
	$y=substr($date,0,4);
	if(checkdate((int)$m,(int)$d,(int)$y)==true){
		$y=(int)$y+543;
		$y=substr($y,2,2);
		$date=adodb_date("d/m",adodb_mktime(0,0,0,$m,$d,0))."/".$y;
		return $date;
	}else{
		return null;
	};
};

function shortthaidbtotime($date){
	$h=substr($date,11,2);
	$m=substr($date,14,2);
		$date=$h.":".$m;
		return $date;
};

function thaidatetodb($date){
        if(empty($date)){
	$now = getdate(time());
	$d=$now["mday"];
	$m=$now["mon"];
	$y=$now["year"];
	$y=$y+543;
	}else{
	$d=substr($date,0,2);
	$m=substr($date,3,2);
	$y=substr($date,6,4);
	};
	$y=(int)$y-543;
	if(checkdate((int)$m,(int)$d,(int)$y)==true){
		$date=adodb_date("m/d/Y",adodb_mktime(0,0,0,$m,$d,$y));
		return $date;
	}else{
		return null;
	};
};

function zthaidatetodb($date){
        if(empty($date)){
	$now = getdate(time());
	$d=$now["mday"];
	$m=$now["mon"];
	$y=$now["year"];
	$y=$y+543;
	}else{
	$d=substr($date,0,2);
	$m=substr($date,3,2);
	$y=substr($date,6,4);
	};
	$y=(int)$y-543;
	if(checkdate((int)$m,(int)$d,(int)$y)==true){
		$date=adodb_date("Y-m-d",adodb_mktime(0,0,0,$m,$d,$y));
		return $date;
	}else{
		return null;
	};
};

function xthaidatetodb($date){
        if(empty($date)){
	$now = getdate(time());
	$d=$now["mday"];
	$m=$now["mon"];
	$y=$now["year"];
	$y=$y+543;
	}else{
	$d=substr($date,0,2);
	$m=substr($date,3,2);
	$y=substr($date,6,4);
	};
	$y=(int)$y-543;
	if(checkdate((int)$m,(int)$d,(int)$y)==true){
		$date=date("m/d/Y",mktime(0,0,0,$m,$d,$y));
		return $date;
	}else{
		return null;
	};
};

function thaimonth($month,$tag){
	if(!empty($month)){
		if($tag=="0"){
			switch($month){
				case "01":$str="ม.ค.";break;
				case "02":$str="ก.พ.";break;
				case "03":$str="มี.ค.";break;
				case "04":$str="เม.ย.";break;
				case "05":$str="พ.ค.";break;
				case "06":$str="มิ.ย.";break;
				case "07":$str="ก.ค.";break;
				case "08":$str="ส.ค.";break;
				case "09":$str="ก.ย.";break;
				case "10":$str="ต.ค.";break;
				case "11":$str="พ.ย.";break;
				case "12":$str="ธ.ค.";break;
			};
		}else{
			switch($month){
				case "01":$str="มกราคม";break;
				case "02":$str="กุมภาพันธ์";break;
				case "03":$str="มีนาคม";break;
				case "04":$str="เมษายน";break;
				case "05":$str="พฤษภาคม";break;
				case "06":$str="มิถุนายน";break;
				case "07":$str="กรกฎาคม";break;
				case "08":$str="สิงหาคม";break;
				case "09":$str="กันยายน";break;
				case "10":$str="ตุลาคม";break;
				case "11":$str="พฤศจิกายน";break;
				case "12":$str="ธันวาคม";break;
			};
		};
		return $str;
	};
};

function shortthaidbtodatestr($date){
	$d=substr($date,8,2);
	$m=substr($date,5,2);
	$y=substr($date,0,4);
	if(checkdate((int)$m,(int)$d,(int)$y)==true){
		$y=(int)$y+543;
		$y=substr($y,2,2);
		$mstr=thaimonth($m,0);
		$date=(int)$d." ".$mstr." ".$y;
		//$date=adodb_date("d/m",adodb_mktime(0,0,0,$m,$d,0))."/".$y;
		return $date;
	}else{
		return null;
	};
};


function datetonumber($date){
	$d=substr($date,8,2);
	$m=substr($date,5,2);
	$y=substr($date,0,4);
        if(checkdate($m,$d,$y)==true){
	$date=date(mktime(0,0,0,$m,$d,$y));
	$date=($date/(60*60*24));
	}else{
	$date ="Invalid";
	};
	return $date;
};

function thaidatetonumber($date){
	$d=substr($date,0,2);
	$m=substr($date,3,2);
	$y=substr($date,6,4);
	$y=(int)$y-543;
        if(checkdate($m,$d,$y)==true){
	$date=mktime(0,0,0,$m,$d,$y);
	$date=($date/(3600*24));
	}else{
	$date ="Invalid";
	};
	return $date;
};

function datenumtodate($date,$num){
	$d=(int)substr($date,0,2);
	$m=(int)substr($date,3,2);
	$y=(int)substr($date,6,4);
	$y=(int)$y-543;
        if(checkdate($m,$d,$y)==true){
	$adate=adodb_date(adodb_mktime(0,0,0,$m,$d,$y));
	$adate=($adate/(60*60*24));
	$bdate=$adate+$num;
	$date=($bdate*60*60*24);
	$now=getdate($date);
	$d=$now["mday"];
	$m=$now["mon"];
	$y=$now["year"];
	$y=$y+543;
	$date=adodb_date("d/m",adodb_mktime(0,0,0,$m,$d,0))."/".$y;
	}else{
	$date ="Invalid";
	};
	return $date;
};

  function strtodbdate($date){
    $a=split("/",$date);
	$d=$a[0];
	$m=$a[1];
	$y=$a[2];
	if(checkdate((int)$m,(int)$d,(int)$y)==true){
		$date1=adodb_date("m/d/Y",adodb_mktime(0,0,0,$m,$d,$y));
		return $date1;
	}else{
		return null;
	};
};
  function xstrthaidate($date){
    $a=split("/",$date);
	$d=$a[1];
	$m=$a[0];
	$y=$a[2]+543;
	if(checkdate((int)$m,(int)$d,(int)$y)==true){
		$date1=$d."/".$m."/".$y;
		//adodb_date("d/m/Y",adodb_mktime(0,0,0,$m,$d,$y+543));
		return $date1;
	}else{
		return null;
	};
};
?>
