Convert Timestamp To Readable Date/Time In PHP


What is PHP ?

Popular server-side scripting language PHP (Hypertext Preprocessor) is mostly used for web development. For building dynamic web pages and applications, it offers a dynamic and interactive environment. Dynamic content can be produced on the server using PHP code that is inserted in HTML markup and performed on the client's web browser. PHP provides a wide range of features, including database connectivity, session management, form handling, and file manipulation, thanks to its rich libraries and frameworks. It is renowned for being straightforward, adaptable, and widely used, making it a top option for creating dynamic websites and web apps.

Convert Timestamp To Readable Date/Time In PHP

Here we can use the DataTime class and dateTime variable to convert the timestamp to a readable date or time in PHP.

Method 1

Using the $dateTime Variable

The transformed value is first stored in a readable format in the dateTime variable before being printed. The value will be stored in date-and-time format in the variable.

Syntax

The syntax of the dateTime variable is as follows:

$dateTime = date('Y-m-d H:i:s', $timestamp);
  • $dateTime is a variable that will store the formatted date and time.

  • date() is a PHP function used to format dates.

  • Y-m-d H:i:s is the format string that specifies how the date and time should be displayed. Let's break it down further:

  • Y represents a four-digit year.

  • m represents a two-digit month.

  • d represents a two-digit day.

  • H represents a two-digit hour in 24-hour format.

  • i represents a two-digit minute.

  • s represents a two-digit second.

  • $timestamp is the UNIX timestamp value that you want to format. It represents the number of seconds since January 1, 1970.

Example

Here is anexample of how you can use dateTime variable.

$timestamp = 1624823400; 
$dateTime = date('Y-m-d H:i:s', $timestamp);
echo $dateTime;

Output

2021-06-27 06:30:00

Explanation of Code

The code seeks to transform a Unix timestamp into a date and time format that is readable by humans. It begins by giving the variable $timestamp a specific timestamp value. The timestamp is then formatted into a readable manner using the date() method. The year, month, day, hour, minute, and second are represented in this situation by the formula 'Y-m-d H:i:s'. The changed time and date are then saved in the variable $dateTime and output using the echo command. With the help of this code, you can quickly transform a timestamp into a format that may be displayed or used for other purposes.

Method 2

Using DateTime() class

This method is similar to the above method, but it uses a class instead of a variable.

Syntax

The syntax for using the DateTime class is as follows:

$dateTime = new DateTime();

You can provide parameters to the DateTime constructor to specify a specific date and time. The constructor accepts an optional string parameter that follows the format 'YYYY-MM-DD HH:MM:SS', where:

YYYY represents the four-digit year.

MM represents the two-digit month.

DD represents the two-digit day.

HH represents the two-digit hour in 24-hour format.

MM represents the two-digit minute.

SS represents the two-digit second.

Example

Here is the example to convert timestamp using the dateTime class.

$timestamp = 1624823400;
$dateTimeObj = new DateTime();
$dateTimeObj->setTimestamp($timestamp);
$dateTime = $dateTimeObj->format('Y-m-d H:i:s');
echo $dateTime;

Output

2021-06-27 06:30:00

Explanation of Code

The transforms a Unix timestamp into a human-readable date and time format using PHP's DateTime class. The first step is to create a fresh instance of the DateTime class and use the setTimestamp() function to set the timestamp. The required date/time format is then specified using the format() method and assigned to the variable $dateTime. Finally, echo $dateTime is used to output the converted date and time. This method offers an alternative method for converting a timestamp into a readable format while utilising PHP's DateTime class's capability.

Conclusion

In conclusion, the tutorial on converting timestamps to readable date/time in PHP provides a comprehensive guide on working with timestamps and the DateTime class. Timestamps are often used to represent dates and times in a numeric format, but they are not easily readable by humans. The tutorial demonstrates how to convert these timestamps into a more human-friendly format using the DateTime class.

Updated on: 28-Jul-2023

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements