PHP
PHP introduction
PHP basics
PHP variables
PHP functions
PHP conditions
PHP loops
PHP arrays
PHP OOP
PHP strings
PHP forms
PHP entitites
PHP files
PHP include files
PHP date and time
PHP cookies
PHP databases
PHP sessions
PHP summary

Programming

Programming intro
Java

Markup

First webpage guide
HTML
XHTML

Browser scripting

Javascript
VBScript
AJAX

Server scripting

ASP

Making money online

Make money online

PHP date and time

PHP provides the functionality to display date and time on a webpage.

This lesson focuses on:

NOTE: Whenever you display the time or date on a webpage using PHP, it will get that data from the web server. If the user has a different time or date set on their computer, PHP will still display the same data from the web server whether it matches up with what the user has on their computer. So if the current date and time on the web server is January 15th 12:30, but the users computer is set to January 10th 8:30, January 15th 12:30 will be displayed. This is in contrast to client-side scripting languages like Javascript which display the data based on what is set on the users computer.

The date() function

The date() function is used in PHP to display time and date on a webpage.

Syntax:

date(string $dateFormat);

The $dateFormat parameter specifies how a date or time should appear on a webpage. You can specify this with a set of characters PHP uses for date and time elements.

Date() function formatting characters:

d

A numeric representation of the day of the month (0 - 31)

m

A numeric representation of the month of the year (01 - 12)

y

A numeric representation (two digit) of a year (99, 00, 01, 02)

H

24 hour format current hour (00 - 23)

i

Minutes (00 - 59)

s

Seconds (00 - 59)

So to display the current date, this code would be used:

<?php
$theDate = date("m/d/y"); 
echo "Today's date: $theDate";
?>

Output:

Today's date: 07/23/08

Timestamps

A timestamp in PHP is the number of seconds that have elapsed since January 1, 1970 00:00. This is also known as the Unix timestamp and it is a widely used standard.

The date() function actually takes two parameters, not one, as mentioned above:

Syntax:

date(string $dateFormat, int $timeStamp);

The second parameter $timeStamp is optional. If you do not set a time stamp when using the date() function, the default timestamp (mentioned above) is used. A timestamp specifies from when the date() function should begin counting. For example, if you use the date() function with a default timestamp to find out the number of years that have elapsed since the timestamp and today, the value returned will be 38 (since 1970).

Creating a timestamp

You can create your own timestamp using the second parameter of the date() function. When you create your own timestamp, the date() function starts counting time from the time specified with your timestamp. If you set a timestamp starting April 1st, 1980 10:15, then the date() function you assign this timestamp to will begin counting from that timestamp. You can set your own timestamp using the mktime() function.

mktime() function syntax:

mktime(int $hour, int $minute, int $second, int $month, int $day, int $year);

Example:

<?php
//create a timestamp starting on 01/19/1988 02:02:00
$newTimeStamp = mktime(02, 02, 00, date("m"), date("d"), date("y") - 20);

//apply this timestamp to the date() function
//which will store its value in the variable $theDate
$theDate = date("m/d/y", $newTimeStamp);
echo ("Current date (if the current year was 1988): $theDate");
?>

Output:

Current date (if the current year was 1988): 07/23/88

Displaying a date on a webpage

You can display a date on a webpage using the appropriate characters for the $dateFormat parameter in the date() function:

<?php
echo "Current date:" . date("m/d/y"); 
?>

Output:

Current date:07/23/08

Displaying time on a webpage

You can display time on a webpage using the appropriate characters for the $dateFormat parameter in the date() function:

<?php
echo "Current time in 12 hour format: " . date("h:i:s A"); 
echo "<br />Current time in 24 hour format: " . date("G:i:s"); 
?>

Output:

Current time in 12 hour format: 05:51:45 PM
Current time in 24 hour format: 17:51:45

Displaying date and time on a webpage

You can display the current date and time together on a webpage using the appropriate characters for the $dateFormat parameter in the date() function:

<?php
echo "Date and time (12 hour format): " . date("m/d/y h:i:s A"); 
echo "<br />Date and time (24 hour format): " . date("m/d/y G:i:s"); 
?>

Output:

Date and time (12 hour format): 07/23/08 05:51:45 PM
Date and time (24 hour format): 07/23/08 17:51:45

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments