Skip to main content

PHP Date and Time


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.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.