note 53676 deleted from function.date by nlopess
| From: | nlopess@php.net | Date: | Sun, 31 Jul 2005 11:48:30 +0000 |
| Subject: | note 53676 deleted from function.date by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-92805@lists.php.net to get a copy of this message | ||
Note Submitter: sunil_ssm at users dot sourceforge dot net
----
The following function makes it easy for you to format mysql datetime and timestamp functions with a
call of a function. This can be achieved with minimal coding as shown below.
Pdeudo Code
------------
$result = mysql_query("SELECT datetime, timestamp FROM log WHERE log_id = 1", $conn);
$rs = mysql_fetch_array($result);
//e.g: 31-12-2005
$formatted_datetime = mysql_datetime($rs["datetime"],"d-m-Y");
//e.g: Sun, 31 December 2005
$formatted_timestamp = mysql_timestamp($rs["datetime"],"D,j F Y");
------------
function mysql_timestamp($stamp, $format) {
$pattern = "/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/i";
if(preg_match($pattern, $stamp, $st) && checkdate($st[2], $st[3], $st[1])) {
return date($format, mktime($st[4], $st[5], $st[6], $st[2], $st[3], $st[1]));
}
return $stamp;
}
function mysql_datetime($datetime, $format) {
$pattern = "/^(\d{4})-(\d{2})-(\d{2})\s+(\d{2}):(\d{2}):(\d{2})$/i";
if(preg_match($pattern, $datetime, $dt) && checkdate($dt[2], $dt[3], $dt[1])) {
return date($format, mktime($dt[4], $dt[5], $dt[6], $dt[2], $dt[3], $dt[1]));
}
return $datetime;
}
------------
Enjoy
Sunil Amber
http://jformmail.sourceforge.net/