The DateFormat function is another really useful function. This is most helpful when using a timestamp from a database. Generally, I use the 'datetime' format from MySQL and the 'smalldatetime' format when using MSSQL. The date is returned as '2007-01-01 00:00:00'. In PHP, the best way to use dates is the Unix timestamp. The Unix timestamp is the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). So right now, the time is '2007-12-16 11:46:56', using Unix, the timestamp the time is '1197823616' or 1197823616 seconds since midnight January 1 1970. This function will still use the date formating as defined in the php function date(). You can check it out at php.net. One little problem with this function is that you are required to send a full date through.
The Function:
<?php
function DateFormat($date,$format)
{
$adbit = explode(" ",$date);
$bdbit = explode("-",$adbit[0]);
if(count($adbit) > 1)
{
$cdbit = explode(":",$adbit[1]);
}
else
{
$cdbit = array();
$cdbit[0] = 0;
$cdbit[1] = 0;
$cdbit[2] = 0;
}
if(strtolower($format) == "short")
{
$dformat = "m/d/Y";
$tformat = "H:i a";
}
elseif(strtolower($format) == "medium")
{
$dformat = "M d, Y";
$tformat = "H:i:s a";
}
elseif(strtolower($format) == "long")
{
$dformat = "F d, Y";
$tformat = "H:i:s a e";
}
elseif(strtolower($format) == "full")
{
$dformat = "l F d, Y";
$tformat = "H:i:s a e";
}
if(isset($dformat) && isset($tformat))
{
$date = date($dformat,mktime($cdbit[0],$cdbit[1],$cdbit[2],$bdbit[1],$bdbit[2],$bdbit[0])) . " " . date($tformat,mktime($cdbit[0],$cdbit[1],$cdbit[2],$bdbit[1],$bdbit[2],$bdbit[0]));
}
else
{
$date = date($format,mktime($cdbit[0],$cdbit[1],$cdbit[2],$bdbit[1],$bdbit[2],$bdbit[0]));
}
return $date;
}
?>
Usage:
<?php
$date = "2007-05-19 13:51:22";
// You can use either
echo DateFormat($date,"F d, Y, g:i a");
// Or you can use
echo DateFormat($date,"long");
?>
The resulting output is: May 19, 2007 1:51 pm
Blogger templates
Blogger news
Blogroll
Archives
-
▼
2012
(231)
-
▼
July
(18)
- How to use Gmail to send email in Opencart
- Domain Model Validation
- What is CakePHP? Why Use it?
- PHP assignment operators
- PHP code for database connection and querry
- How to hack facebook accounts
- PHP Switch Statement
- DateFormat
- No title
- Real Estate Script, Real Estate Software, PHP Scri...
- Assignment#2
- Outsourcing PHP Projects
- PHP if statement
- Url Encode and Url Decode
- HTTP Headers
- Date() Functions
- Ways To Earn With AIM Global
- foreach() in php
-
▼
July
(18)