PHP
  1. PHP intro
  2. PHP basics
  3. PHP variables
  4. PHP functions
  5. PHP conditions
  6. PHP loops
  7. PHP arrays
  8. PHP classes & objects
  9. PHP strings
  10. PHP forms
  11. PHP entities
  12. PHP files
  13. PHP include files
  14. PHP date & time
  15. PHP cookies
  16. PHP databases
  17. PHP sessions
  18. PHP summary

PHP date and time

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

This tutorial 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:

Displaying the current date example:
<?php $theDate = date("m/d/y"); echo "Today's date: $theDate"; ?>
Output:
Today's date: 03/29/17

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 can actually take two parameters - adate format and a time stamp

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): 12/31/69

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:

Example:
<?php echo "Current date:" . date("m/d/y"); ?>
Output:
Current date:03/29/17

Displaying time on a webpage

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

Example:
<?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: 12:13:31 AM
Current time in 24 hour format: 0:13:31

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:

Example:
<?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): 03/29/17 12:13:31 AM
Date and time (24 hour format): 03/29/17 0:13:31
© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information