December 13, 2015

Formatting Date in PHP from MySQL


Formatting date is one area where most PHP beginners face challenge. One thing that we ought to understand is that PHP and MySQL store dates in different formats. Where PHP uses Day,Month and Year in that order of arrangement; MySQL does the reverse of that. It starts with Year, Month and Day. This makes things a little bit confusing. Understanding their default formats will help you achieve whatever you want to use the date stored in the database to achieve whatever you want.

The benefit of this is that, dates can be displayed in a user-friendly manner that can be understood by virtually anybody.

The criteria to use this PHP functionality is to declare the MySQL column either as DATETIME or DATE data-type during table creation

The following will normalize the MySQL date format:
$defaultdate = strtotime($getmysqldefaultdate);
$mydatestyle = date( 'Y-m-d H:i:s', $defaultdate);

The line
$defaultdate = strtotime($getmysqldefaultdate);
accepts a date string (probably from MySQL database; $getmysqldefaultdate denotes that variable) and performs a series of operations to turn that string into UNIX timestamp. This is achieved using the
strtotime(date vairable);
PHP function.
The line below does the little magic that presents your date and time in a friendly manner.
$mydatestyle = date('Y-m-d H:i:s', $defaultdate);
It uses that timestamp and PHP's date function to turn that timestamp back into MySQL's standard date format.
Here is what each format character in the string above represents:

  • l = Full name for day of the week (lower-case L).
  • F = Full name for the month.
  • j = The day of the month.
  • Y = The year in 4 digits. (lower-case y gives the year's last 2 digits)
  • m = Numeric with leading zero.
  • d = Numeric with leading zero.
  • H = 24 hour time. numeric with leading zero.
  • i = Minute, numeric with leading zero.
  • s = Seconds; numeric with leading zero.
Considering the day and time of this post; our displayed date and time will be:


The table below will help you to format date into any style of your choice.

Day of Month
dNumeric, with leading zeros01-31
jNumeric, without leading zeros1-31
SThe English suffix for the day of the month st, nd or th in the 1st, 2nd or 15th.
Weekday
lFull name  (lowercase 'L')Sunday - Saturday
DThree letter nameMon - Sun
Month
mNumeric, with leading zeros01-12
nNumeric, without leading zeros1-12
FTextual fullJanuary - December
MTextual three lettersJan - Dec
Year
YNumeric, 4 digitsEg., 1999, 2003
yNumeric, 2 digitsEg., 99, 03
Time
aLowercaseam, pm
AUppercaseAM, PM
gHour, 12-hour, without leading zeros1-12
hHour, 12-hour, with leading zeros01-12
GHour, 24-hour, without leading zeros0-23
HHour, 24-hour, with leading zeros00-23
iMinutes, with leading zeros00-59
sSeconds, with leading zeros00-59
TTimezone abbreviationEg., EST, MDT ...
Full Date/Time
cISO 86012004-02-12T15:19:21+00:00
rRFC 2822Thu, 21 Dec 2000 16:01:07 +0200 



The table above will help you solve almost every problem related to date.

0 comments:

Post a Comment