PHP date and time

Home - Tutorials - Date and time

In this article I will show you how to use the PHP date and time functions and how to format date and time output.

Tutorial info:

Name:PHP date and time
Total steps:1
Category:Date and time

Bookmark PHP date and time

Bookmark and Share

Step 1 - PHP date and time basics

PHP date and time

PHP date and time handling functions allows you to get the date and time from the server where the script is executed. Besides this you can manipulate, edit, format these values before displaying them. In this section I will show you the most important functions how to get date and time.

The PHP date() function 

The most used function in date and time handling is the date() itself. This function returns with the actual date and time in the format you specified. However the function is able to convert a timestamp format into a human readable format as well. The complete syntax of the function is the following:

string date ( string $format [, int $timestamp] )

You can get the actual date and time information as follows:

  1. <?php
  2. echo "<br/>Display date: ";
  3. echo date('Y-m-d');
  4. echo "<br/>Display time: ";
  5. echo date('H:i:s');
  6. echo "<br/>Display date and time: ";
  7. echo date('Y-m-d H:i:s');
  8. echo "<br/>Display date and time in long format: <br/>";
  9. echo date('Y F d, D - g:i:s A');
  10. ?>
Display date: 2007-09-12 Display time: 14:34:21 Display date and time: 2007-09-12 14:34:21 Display date and time in long format: 2007 September 12, Wed - 2:34:21 PM

The complete list of available formatting parameters can be found in the date manual.


The mktime() function 

Sometimes you want to get a timestamp in the past or in the future. You can do this using the mktime function. You need to specify the time and it returns with the Unix timestamp. The syntax is the following:

int mktime ( [int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year [, int $is_dst]]]]]]] )

Later you can use this timestamp in the above mentioned date function to display it in a format you want. So you can use it as like here:

  1. <?php
  2. echo "<br/>Date generated by mktime:<br/>";
  3. echo date('Y-m-d',mktime(0,0,0,5,15,2006));
  4. echo "<br/>Date generated by mktime with invalid day and month:<br/>";
  5. echo date('Y-m-d',mktime(0,0,0,15,35,2006));
  6. ?>
Date generated by mktime: 2006-05-15 Date generated by mktime with invalid day and month: 2007-04-04

So if you want to get the tomorrows date you can use the following code:

  1. <?php
  2. echo "Today is: ".date('Y-m-d')."<br/>";
  3. echo "Tomorrow is: ";
  4. echo date('Y-m-d',mktime(0,0,0,date('m'),date(d)+1,date('Y')));
  5. ?>
Today is: 2007-09-12 Tomorrow is: 2007-09-13

Here we used mktime and added 1 day to the day parameter. 

Tags: php date, php time, dat time, php datetime, date functions, time functions

PHP date and time - Table of contents
Step 1 - PHP date and time basics

Follow phpf1 on Twitter

F1 Site Family
Java F1
JavaScript F1

Total time: 0.017