go top

MEGA Sale

  • Colection of 65 PHP scripts for $4.29 each

Get 65 PHP scripts in a bundle for $4.29 each!

View Offer

Date and time formatting with PHP

Tuesday, 6th December, 2011  /  PHP Tutorials  /  11 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.
Share on:

11 Comments to "Date and time formatting with PHP"

phptechie

phptechie / August 20, 2020 at 14:10 pm

Excellent tutorial and very well explained! Very useful for the beginners. Thanks a lot.

Webetutorial

Webetutorial / July 16, 2018 at 17:47 pm

Nice post, this is very helpful post.

benny

benny / July 18, 2014 at 21:13 pm

How? to textbox always blank
If not textbox anable:

<div class="element-date"><label class="title"><?php echo $_SESSION['Tgl'];?></label><div class="item-cont"><input class="medium" data-format="yyyy-mm-dd" type="date" name="Tgl" value="<?php echo $_SESSION['Tgl']; ?>" /><span class="icon-place"></span></div></div>

dhanilbs

dhanilbs / January 11, 2014 at 07:12 am

php is difficult

Pr

Pr / November 24, 2019 at 06:20 am

It is not difficult

Kashif

Kashif / August 13, 2013 at 18:17 pm

Really very help full article.
Thanks

LyZzard

LyZzard / April 8, 2013 at 00:04 am

Here is an example how to manipulate and obtain needed date for your form.

echo date("Y-m-d", strtotime("+8hour"));

hasit

hasit / April 10, 2012 at 03:09 am

i learn so many things from tis web about php..
thanx!!!

Dominor Novus

Dominor Novus / May 10, 2011 at 21:01 pm

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!

Arvind Barge

Arvind Barge / August 17, 2020 at 00:54 am

Thanks

Vishal

Vishal / August 11, 2009 at 18:03 pm

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.

Add your comment

Captcha

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

    Log in to your account to post your comments. If you still haven't joined our community yet, 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

    Free Scripts

    Add great new functionalities to your website with our Free Scripts collection.


    Free scripts

    PHP Scripts

    Check our extensive collection of top-notch PHP Scripts that will enhance your website!


    Commercial PHP scripts