I'm creating one simple blog script and I have this problem:
In MySQL I'm saving PostDate
as TIMESTAMP
, and when I select from MYSQL PostDate
looks like this 2015-10-25 13:53:35
, but I want to convert this timestamp to H:M D-M-Y
. I'm created one function for converting but I don't know how to use it for key PostDate
in array. Exactly I want to rewrite all PostData
keys in array to new format. I know how to do it with foreach
loop but I cant do this because I use Smarty Template Engine.
ConvertTimestamp():
public function ConvertTimestamp($Timestamp)
{
$TimeDate = explode(' ', $Timestamp);
$Date = explode('-', $TimeDate['0']);
$Date = $Date[2].'-'.$Date['1'].'-'.$Date['0'];
$Time = explode(':', $TimeDate['1']);
$Time = $Time['0'].':'.$Time['1'];
$TimeDate = $Time.' '.$Date;
return $TimeDate;
}
this format 2015-10-25 13:53:35
is called datetime format not TIMESTAMP .
But anyway , to convert datetime to any format you want you can do this at mysql side . Suppose the table you select the data from is called "test_table" and has a datetime field called PostDate in that table which you want to convert to this format H:M D-M-Y
( which is what you want ) , you can do the following query :
SELECT DATE_FORMAT(PostDate,'%H:%M %D-%M-%Y') FROM `test_table`