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 | ||
|---|---|---|
| d | Numeric, with leading zeros | 01-31 |
| j | Numeric, without leading zeros | 1-31 |
| S | The English suffix for the day of the month | st, nd or th in the 1st, 2nd or 15th. |
| Weekday | ||
| l | Full name (lowercase 'L') | Sunday - Saturday |
| D | Three letter name | Mon - Sun |
| Month | ||
| m | Numeric, with leading zeros | 01-12 |
| n | Numeric, without leading zeros | 1-12 |
| F | Textual full | January - December |
| M | Textual three letters | Jan - Dec |
| Year | ||
| Y | Numeric, 4 digits | Eg., 1999, 2003 |
| y | Numeric, 2 digits | Eg., 99, 03 |
| Time | ||
| a | Lowercase | am, pm |
| A | Uppercase | AM, PM |
| g | Hour, 12-hour, without leading zeros | 1-12 |
| h | Hour, 12-hour, with leading zeros | 01-12 |
| G | Hour, 24-hour, without leading zeros | 0-23 |
| H | Hour, 24-hour, with leading zeros | 00-23 |
| i | Minutes, with leading zeros | 00-59 |
| s | Seconds, with leading zeros | 00-59 |
| T | Timezone abbreviation | Eg., EST, MDT ... |
| Full Date/Time | ||
| c | ISO 8601 | 2004-02-12T15:19:21+00:00 |
| r | RFC 2822 | Thu, 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