« PHP validation and verificationDate and time manipulations with PHP »

Date and time formatting with PHP Posted in PHP Tutorials | 6 Comments
Hello again. Today's tutorial is separated into two parts. In the first part we will get familiar with date and time formatting and in the second we will exercise date and time manipulations. Probably most of you are familiar with formatting, but for those who are not and for those that would like to remind of it please read below.

You may think that date and time topic is not very important in web applications and you'll be wrong. Almost every one has it. It may be not so obvious, it may be even hidden from the regular users, but it is there. Just think about it for a second. Where do we always use date and time? ... If you still haven't got the answer here it is. The most common place for dates and time are databases and log files. Why? We all want to have a log of users' actions. We log everything so we can keep track of what happens, because one day it will inevitably go wrong and we would like to now what caused the problem. The main part of every log is "when it happened". It may be only the administrator of the web application, who is reading it, or it can be hundreds of company's employees that carefully sort every event that took part, but they all need to know "date & time". You may still ask yourself why I started with User Interface but take a look at this:

1202395558

Not many people can read this clearly without any problems. This is the current server time. Now see this:

2008-02-07 16:45:58
Feb 07 08 4:45:58 pm

For those of you that are not genius enjoying themselves by calculating big integer time, I will reveal the mystery of how we can format dates and time. We will begin with dates formatting. It is done using date() function. It accepts two parameters. Format string and time as integer. We will use time() for current time integer. Let's see some examples.

echo date("d-m-y", time());     // 07-02-08


The dash between the characters in the format string above is exactly what the output is going to be separated with.

echo date("D j/n/Y", time());     // Thu 7/2/2008


The dash here is replaced with forward slash.

echo date("jS of F Y", time());     // 7th of February 2008


We can use any character we want here, just keep in mind that if it has a special meaning it has to be escaped. And just to be sure that there are no mistakes I suggest that you always escape every character you use.

echo date("d M y", time());     //07 Feb 08


Formatting the date using short representation for month names and short year.

echo date("l jS of F", time());     // Thursday 7th of February


Full day and month name with ordinal suffix for the day of the month.

Date format characters' legend by examples:

d - Numeric representation of a day, with leading zeros 01 through 31.
m - Numeric representation of a month, with leading zeros 01 through 12.
y - Numeric representation of a year, two digits.

D - Textual representation of a day, three letters Mon through Sun.
j - Numeric representation of a day, without leading zeros 1 through 31.
n - Numeric representation of a month, without leading zeros 1 through 12.
Y - Numeric representation of a year, four digits.

S - English ordinal suffix for the day of the month. Consist of 2 characters st, nd, rd or th.
F - Textual representation of a month, January through December.

M - Textual representation of a month, three letters Jan through Dec.

l textual representation of the day of the week Sunday through Saturday.
Now we move on to our next task which is time formatting. We are still going to use the same date() function, but with different format characters.

echo date("G:i:s", time());     //16:45:58


Working with time is a little bit simpler, because there is only one way to represent minutes and seconds.

echo date("H:i:s", time());    //16:45:58


Despite of the very few options for minutes and seconds formats we have a lot of hour formatting styles. This one represents hours with leading zeroes 0 through 23.

echo date("g:i a.", time());     //4:45 pm.


This is an example of time format with lowercase ante meridiem and post meridiem (am/pm).

echo date("h:i A.", time());     //04:45 PM.


This is an example of time format with uppercase AM/PM.
Time format characters' legend by examples:

G 24-hour format of an hour without leading zeros 0 through 23.
i Numeric representation of minutes with leading zeros 00 through 59.
s Numeric representation of seconds with leading zeros 00 through 59.
H 24-hour format of an hour with leading zeros 00 through 23.
a Lowercase Ante meridiem and Post meridiem am or pm.
g 12-hour format of an hour without leading zeros 1 through 12.
A Uppercase Ante meridiem and Post meridiem AM or PM.
h 12-hour format of an hour with leading zeros 01 through 12.
And when we combine both date and time formatting we get some really nice looking output strings.

echo date("l jS of F g:i A.", time());     // Thursday 7th of February 4:45 PM.


echo date("D M j G:i:s T Y", time());     // Thu Feb 7 16:45:58 EET 2008


Now take a deep breath, get some chips and get ready because we are headed for the second part of our tutorial which is date time manipulations.

Do you know PHP / HTML / CSS / JS well?

Write tutorial on a topic you are good in and become a trusted PHP jabber! Share your knowledge with thousands of webmasters and we will reward you for your generosity by giving you bonus points which you can use as a voucher to buy any of our commercial products. Read more about our reward program.

6 Replies to "Date and time formatting with PHP"

dhanilbs January 11, 2014 at 7:12 am | Reply

+1

php is difficult
Kashif August 13, 2013 at 6:17 pm | Reply

+1

Really very help full article.
Thanks
LyZzard April 8, 2013 at 12:04 am | Reply

+2

Here is an example how to manipulate and obtain needed date for your form.
echo date("Y-m-d", strtotime("+8hour"));
hasit April 10, 2012 at 3:09 am | Reply

+5

i learn so many things from tis web about php..
thanx!!!
Dominor Novus May 10, 2011 at 9:01 pm | Reply

+4

I was trying to remember the swiftest way to format the value of a text box derived from a date picker.

$datepickerbox = $_POST[\'datepickerbox\'];
$datepickerbox = date(\"Y-m-d H:i:s\", time($datepickerbox));
echo $datepickerbox;

Cheers for the concise reminder!
Vishal August 11, 2009 at 6:03 pm | Reply

+2

I am having real trouble trying to understand how can I display specified input time + 8 hours.

On our site, software builds static pages from database about ever 8 hours. I can easily insert last build time (yyyy-mm-dd hh:mm:ss), however don't have any clue on how can I add 8 hours to this.

Is there any way to add +8 to hh and also make yyyy-mm-dd to react to it? so it won't mess up the output because of the change in date, month or year?

If you can help, it would be really great.
  • 1


Please be polite and helpful and do not spam or offend others. We promise you will be treated the same way.

Log in your free account or if you still haven't joined our Webmaster Community Reward Program, you can create your free account now.

Posting tip:
if you use code in your comments please put it in these tags [php], [sql], [css], [js]
PHP code example: [php] echo date("Y-m-d"); [/php]

Thank you,
~ PHPJabbers team ~