PHP Date Time

Summary: in this tutorial, you will learn how to handle dates and times using some useful PHP date and time functions.

Introduction to UNIX timestamp

Computer systems store a date and time as a UNIX timestamp, or just a timestamp. A timestamp is an integer that refers to the number of seconds between 1970-01-01 00:00:00 UTC and the date and time to be stored.

Computers stores dates and times as timestamps because of the following reasons:

  • First, it is easier for the computer to store an integer.
  • Second, it is easier to manipulate. For example, to add one day to a timestamp, it just adds the number of seconds i.e., 24x60x60 = 86400 seconds.

A UNIX timestamp is timezone-independent. It means if you calculate the timestamp at the same time on different computers in different time zones, you get the same value.

PHP provides various dates and times functions that manipulate timestamps directly.

PHP getting current date and time

To get current data and time that the web server stores, you use the time() function as follows:

The time() function returns the current time as a timestamp so it is not human-readable. Fortunately, PHP provides several functions that help you format the timestamps as human-readable strings.

You can use the date() function that formats a timestamp as a formatted date and time string as following:

The default value of the second parameter of the date() function is the current date and time so you can omit it as follows:

The following example shows you how to display the tomorrow’s date and time:

For more information on the format of the date and time, please refer to the PHP date function.

PHP creating timestamps

PHP provides some useful functions for creating timestamps. The following functions are the commonly used functions: mktime()gmmktime() and strtotime().

Creating timestamps from date and time values

The mktime() function returns a timestamp based on up to six arguments: year(1901-2038), month(1-12), day(1-31), hour(0-24), minute(0-59) and second(0-59).

The following example displays the timestamp corresponding to 1:30:00 AM on January 01, 1970:

To create a timestamp from a GMT date and time, you use the gmmktime() function.

mktime() vs. gmmktime()

Both the mktime() and gmmktime() function returns a UNIX timestamp based on the same input arguments. However the mktime() function returns a timezone-specific value, while gmmktime() function returns the same timetamp everywhere.

Creating timestamps from date and time strings

PHP provides the strtotime() function that allows you to convert a date and time string into a timestamp, which is very useful in some cases. With the strtotime() function, you can pass in almost any human-readable format e.g., ” and get the corresponding timestamp value:

Check it out the strtotime() function for more information.

PHP getting date and time from a timestamp

To get date and time values from a timestamp, you use the getdate() function. The getdate() function returns an associative array that contains date and time values of the input timestamp. Interestingly, the default value of the timestamp parameter is the now, or time() function.

Let’s take a look at the following example:

The following is the output of the script at the time it runs:

PHP date time - getdate() output

The following example displays the UNIX time using the getdate() function:

In this tutorial, we’ve discussed how computers store dates and times as timestamps. We also introduced some commonly used PHP data and time functions that help you handle dates and times effectively.

  • Was this tutorial helpful ?
  • YesNo