PHP Date and Time

Submitted by: 
Language: 
Visitors have accessed this post 2132 times.

In this tutorial, we are going to learn about PHP Date and Time. Date and time are part of our everyday lives. PHP also provides this tools to make it work the dates and time easier. These functions allow you to get the date and time from your server where the PHP scripts are running. Then, you can use these functions to format the date and time in some ways.

Note: These function it depends on your settings of your server.

Installation: No need installation for these functions.

The PHP Date() Function

The PHP date() function formats a timestamp, so they are more likely human readable.

Syntax

date(format,timestamp)

format - This is required field and the timestamp must be in specific format.
timestamp - This is optional field and it also specific timestamp and the current date and time is default.

Simple Date

Here are commonly used of characters for dates:

d - stand for the days of the month (01 - 31)
m - stand for a month (01 - 12)
Y - stand for a year; like (2010) (four digits)
l (this is lowercase of L) - stand for the days of the week

And, there are other characters can also be used for adding additional format in dates like "/", ".", or "-".

Example

This is the image result of the code below:

date

  1. <?php
  2. echo "Today is " . date("l") . "<br>";
  3. echo "The date today is " . date("Y/m/d") . "<br>";
  4. echo "The date today is " . date("Y-m-d") . "<br>";
  5. echo "The date today is " . date("Y.m.d");
  6. ?>

Another example, these function used to automatic update the copyright year of your website.

This is the image result of the code below:

copyright

Copyright &copy; <?php echo date("Y"); ?>, Free source code, tutorials and articles

Simple Time

Here are commonly used of characters for times:

h - stand for 12-hour format (01 - 12)
i - stand for minutes (00 - 59)
s - stand for seconds (00- 59)
a - stand for Ante meridiem and Post meridiem (am or pm)

Example

This is the image result of the code below:

time

  1. <?php
  2. echo "The default time is " . date("h:i:s a");
  3. ?>

Date and Time Example

This is the image result of the code below: Date and Time codes.

date and time

  1. <?php
  2. echo "The date today is " . date("Y-m-d") . " and the time is " . date("h:i:s a");
  3. ?>

As of now this is the basic syntax of PHP Date and Time. As we go on we will teach you on how to create PHP Include Files one by one.

So what can you say about this work? Share your thoughts in the comment section below or email me at [email protected]. Practice Coding. Thank you.


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.