When developing websites and applications, we often need to deal with time and date. In PHP, timestamp is a very common time format, which represents the number of seconds that have elapsed since January 1, 1970, 0:00:00 (UTC). However, the time format displayed by Unix timestamp is not human-friendly, so in practical applications we need to convert it into a more readable date and time format. This article will explain how to convert Unix timestamp to common date and time formats in PHP.
- Convert Unix timestamp to date
In PHP, we can use the date function to convert Unix timestamp to date. The date function accepts two parameters: one is the date format and the other is the Unix timestamp. Here is an example:
$timestamp = time(); //获取当前时间的时间戳 $date = date('Y-m-d', $timestamp); //将时间戳转换为日期格式 echo $date; //输出2021-06-14
In the above example, we used the time function to get the Unix timestamp of the current time, and then used the date function to convert it to the year-month-day date format. If you want to convert the timestamp to other formats, you can check PHP's official documentation and modify it as needed.
- Convert Unix timestamp to time
Similar to date, we can also use the date function to convert Unix timestamp to time format. The following is an example:
$timestamp = time(); //获取当前时间的时间戳 $time = date('H:i:s', $timestamp); //将时间戳转换为时间格式 echo $time; //输出21:29:50
In the above example, we also use the time function to obtain the Unix timestamp of the current time, and then use the date function to convert it into the hour:minute:second time format. If you want to convert the timestamp to other formats, you can also check PHP's official documentation and modify it as needed.
- Convert Unix timestamp to date time
If we need to convert Unix timestamp to a more precise date time format, we can Date and time formats are used in combination. The following is an example:
$timestamp = time(); //获取当前时间的时间戳 $date_time = date('Y-m-d H:i:s', $timestamp); //将时间戳转换为日期时间格式 echo $date_time; //输出2021-06-14 21:35:40
In the above example, we combine the date and time formats and use the date function to convert the Unix timestamp to year-month-day hour:minute:second. Date time format. Similarly, if you want to convert the timestamp to another format, you can check PHP's official documentation and modify it as needed.
- Convert date time to Unix timestamp
In addition to converting Unix timestamp to date time format, we sometimes also need to convert date time Format converted to Unix timestamp. In PHP, we can convert datetime format to Unix timestamp using strtotime function. Here is an example:
$date_time = '2021-06-14 21:35:40'; //待转换的日期时间 $timestamp = strtotime($date_time); //将日期时间转换为Unix时间戳 echo $timestamp; //输出1623692140
In the above example, we define the datetime format as a string and then use the strtotime function to convert it to a Unix timestamp. Similarly, if you want to convert other date and time formats into Unix timestamps, you can check PHP's official documentation and modify it as needed.
Summary
This article introduces how to convert Unix timestamp to common date and time formats in PHP, and how to convert date time format to Unix timestamp . Mastering this knowledge allows us to handle time and dates more conveniently, making development work more efficient. Of course, there are many other techniques and knowledge for processing time and date, and I hope readers can study them in depth and master them.
The above is the detailed content of How to convert unix timestamp in php. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.