User define PHP date function


Sometimes we need to display date in different format as user requirements. Here some PHP user define date function i have used. I think it will be helpful for PHP professionals. As you can see in my all example there are tons of different formats that can be used in the date feature.

<?php
/**
* mysql::cDateMySQL2Br()
* convert date from YYYY/MM/DD to DD/MM/YYYY
*/
function cDateMySQL2Br($dt) {

if (strlen($dt) > 10) $dt = substr($dt,0,10); //despreza a hora

if (strlen($dt) == 10) {
$nYear = substr($dt,0,4); $nMonth = substr($dt,5,2); $nDay = substr($dt,8,2);
if (checkdate($nMonth, $nDay, $nYear)) $sDate = “$nDay/$nMonth/$nYear”;
}
return $sDate;
}

/**
* mysql::cDateTimeMySQL2Br()
* convert date time from YYYY/MM/DD HH:MM:SS to DD/MM/YYYY HH:MM:SS
*/
function cDateTimeMySQL2Br($dt) {

if (strlen($dt) > 10) {
$tm = substr($dt,11, strlen($dt)-11);
$dt = substr($dt,0,10);
}

if (strlen($dt) == 10) {
$nYear = substr($dt,0,4); $nMonth = substr($dt,5,2); $nDay = substr($dt,8,2);

if (checkdate($nMonth, $nDay, $nYear)) $sDate = “$nDay/$nMonth/$nYear”;
}
if ($tm != “” && $tm != “00:00:00”) $sDate = “$sDate $tm”;
return $sDate;
}

/**
* mysql::cDateBr2MySQL()
* convert date from DD/MM/YYYY to YYYY/MM/DD
*/
function cDateBr2MySQL($dt) {

if (strtoupper($dt) == “NULL”) $sDate = $dt;
if (strlen($dt) > 10) $dt = substr($dt,0,10); //despreza a hora

if (strlen($dt) == 10) {
if ($debug) echo “Converting date: Portuguese[$dt] for “;
$nYear = substr($dt,6,4); $nMonth = substr($dt,3,2); $nDay = substr($dt,0,2);
if ($debug) echo “MySQL[$nYear/$nMonth/$nDay] <br>\n\n”;

//if (checkdate($nMonth, $nDay, $nYear))
$sDate = “$nYear/$nMonth/$nDay”;
} else {
if ($debug) echo “Date format inválid [$dt]. Use dd/mm/YYYY <br>\n\n”;
}
return $sDate;
}

/**
* mysql::cDateTimeBr2MySQL()
* convert date time from DD/MM/YYYY HH:MM:SS to YYYY/MM/DD HH:MM:SS
*/
function cDateTimeBr2MySQL($dt) {

if (strtoupper($dt) == “NULL”) $sDate = $dt;
if (strlen($dt) > 10) {
$tm = substr($dt,11,strlen($dt)-11);
$dt = substr($dt,0,10);
}

if (strlen($dt) == 10) {
if ($debug) echo “Converting date: Portuguese[$dt] for “;
$nYear = substr($dt,6,4); $nMonth = substr($dt,3,2); $nDay = substr($dt,0,2);
if ($debug) echo “MySQL[$nYear/$nMonth/$nDay] <br>\n\n”;

//if (checkdate($nMonth, $nDay, $nYear))
$sDate = “$nYear/$nMonth/$nDay”;
} else {
if ($debug) echo “Date format inválid [$dt]. Use dd/mm/YYYY <br>\n\n”;
}

if ($tm != “” && $tm != “00:00:00”) $sDate = “$sDate $tm”;

return $sDate;
}

/**
* mysql::fullDateFormat()
* convert date from YYYY-MM-DD to May 3, 2008
*/
function fullDateFormat( $value ){
$d = explode(“-“, $value);
$cdate = date (“F j, Y”, mktime (0,0,0,$d[1],$d[2],$d[0]));
return $cdate;
}

/**
* mysql::panFormatDate()
* convert date from %Y-%m-%d %H:%M:%S to Monday, 17 March 2008
*/
function panFormatDate( $date, $format=””, $offset=NULL ){
if ( $format == ” ) {
// %Y-%m-%d %H:%M:%S
$format = “%A, %d %B %Y”;
}
if ( is_null($offset) ) {
$offset = 0;
}
if ( $date && ereg( “([0-9]{4})-([0-9]{2})-([0-9]{2})[ ]([0-9]{2}):([0-9]{2}):([0-9]{2})”, $date, $regs ) ) {
$date = mktime( $regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1] );
$date = $date > -1 ? strftime( $format, $date + ($offset*60*60) ) : ‘-‘;
}
return $date;
}
?>

Advertisements

One thought on “User define PHP date function

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s