Home  >  Article  >  Backend Development  >  How to convert the current timestamp in php

How to convert the current timestamp in php

PHPz
PHPzOriginal
2023-03-29 10:11:59360browse

PHP Current Timestamp Conversion: Convert a timestamp into a readable date and time format

In PHP, a timestamp is an integer representing the number of seconds that have elapsed since January 1, 1970 . Timestamps are very commonly used in web development because they can help us pinpoint events, calculate time differences, and better process time data. However, the integer form of the timestamp is not convenient for us to use directly because it is not a readable date and time format, such as 2019-11-05 16:28:45.

Therefore, in actual development, it is often necessary to convert timestamps into date and time formats that are easy to read and understand. Next, we'll cover how to use PHP to convert timestamps into common date and time formats.

  1. Convert timestamp to date format

In PHP, you can use the date() function to convert timestamp to date format. The syntax is as follows:

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

Among them, the $format parameter represents the date format you want to obtain, and the $timestamp parameter represents the timestamp to be converted. If the $timestamp parameter is not provided, the current time is used by default.

Here are some examples of common date formats:

$timestamp = 1572925805; // 2019-11-05 16:30:05

echo date("Y-m-d", $timestamp); // 2019-11-05

echo date("F j, Y", $timestamp); // November 5, 2019

echo date("l, F j, Y", $timestamp); // Tuesday, November 5, 2019

echo date("M j, Y G:i:s", $timestamp); // Nov 5, 2019 16:30:05
  1. Convert timestamp to time format

In addition to date format, timestamp can also Convert to time format. Also using the date() function, just set the $date parameter to the time format you want.

Here are some examples of time formats:

$timestamp = 1572925805; // 2019-11-05 16:30:05

echo date("H:i:s", $timestamp); // 16:30:05

echo date("g:i a", $timestamp); // 4:30 pm

echo date("h:i:s A", $timestamp); // 04:30:05 PM
  1. Convert timestamp to date and time format

In actual development, we may need Display date and time simultaneously, not just one format. In order to achieve this goal, we can use the combined form of the date() function to add the date and time format to $format together.

The following are some examples of date and time formats:

$timestamp = 1572925805; // 2019-11-05 16:30:05

echo date("Y-m-d H:i:s", $timestamp); // 2019-11-05 16:30:05

echo date("F j, Y, g:i a", $timestamp); // November 5, 2019, 4:30 pm

echo date("D, jS M Y, h:i A", $timestamp); // Tue, 5th Nov 2019, 04:30 PM
  1. Convert timestamp to local time

In the above example, the timestamp represents is UTC time. If you want to convert it to local time, you can use the date_default_timezone_set() function or the ini_set() function to set the time zone, and use the $timestamp parameter in the date() function. As shown below:

$timestamp = 1572925805; // 2019-11-05 16:30:05

date_default_timezone_set('Asia/Shanghai');
echo date('Y-m-d H:i:s', $timestamp); // 结果为本地时间

With the above method, you can easily convert the timestamp into a readable date and time format so that you can process time data. In actual development, such conversion is very useful and can improve work efficiency and code readability.

The above is the detailed content of How to convert the current timestamp in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn