| 제목 | MySQL date/datetime 핸들링 | ||
|---|---|---|---|
| 글쓴이 | 듬직이 | 작성시각 | 2009/09/12 22:33:50 |
|
|
|||
|
질문 게시판에 아카르트 님의 질문을 보고 도움이 될까 해서 적어 봅니다. 출처 : http://codeigniter.com/forums/viewthread/46720/#When:18:10:08Z
/**
* Convert MySQL's DATE (YYYY-MM-DD) or DATETIME (YYYY-MM-DD hh:mm:ss) to timestamp
*
* Returns the timestamp equivalent of a given DATE/DATETIME
*
* @todo add regex to validate given datetime
* @author Clemens Kofler <clemens.kofler@chello.at>
* @access public
* @return integer
*/
function mysqldatetime_to_timestamp($datetime = "")
{
// function is only applicable for valid MySQL DATETIME (19 characters) and DATE (10 characters)
$l = strlen($datetime);
if(!($l == 10 || $l == 19))
return 0;
//
$date = $datetime;
$hours = 0;
$minutes = 0;
$seconds = 0;
// DATETIME only
if($l == 19)
{
list($date, $time) = explode(" ", $datetime);
list($hours, $minutes, $seconds) = explode(":", $time);
}
list($year, $month, $day) = explode("-", $date);
return mktime($hours, $minutes, $seconds, $month, $day, $year);
}
/**
* Convert MySQL's DATE (YYYY-MM-DD) or DATETIME (YYYY-MM-DD hh:mm:ss) to date using given format string
*
* Returns the date (format according to given string) of a given DATE/DATETIME
*
* @author Clemens Kofler <clemens.kofler@chello.at>
* @access public
* @return integer
*/
function mysqldatetime_to_date($datetime = "", $format = "d.m.Y, H:i:s")
{
return date($format, mysqldatetime_to_timestamp($datetime));
}
/**
* Convert timestamp to MySQL's DATE or DATETIME (YYYY-MM-DD hh:mm:ss)
*
* Returns the DATE or DATETIME equivalent of a given timestamp
*
* @author Clemens Kofler <clemens.kofler@chello.at>
* @access public
* @return integer
*/
function timestamp_to_mysqldatetime($timestamp = "", $datetime = true)
{
if(empty($timestamp) || !is_numeric($timestamp)) $timestamp = time();
return ($datetime) ? date("Y-m-d H:i:s", $timestamp) : date("Y-m-d", $timestamp);
}
|
|||
| 태그 | mysql,date,datetime | ||
| 다음글 | plugin으로 interface정의 해서 사용하기. | ||
| 이전글 | euc-kr 환경에서 uri에 한글 사용하기 (4) | ||
|
emc
/
2009/09/13 12:20:22 /
추천
0
간단한 설명이라도 써주시면 더 좋을것 같습니다.
|