Home >Backend Development >PHP Problem >How to convert timestamps to each other in php

How to convert timestamps to each other in php

PHPz
PHPzOriginal
2023-03-29 10:13:561088browse

In the process of developing websites or Internet applications, we often need to convert any time into a timestamp or convert a timestamp into a specific time. PHP is a widely used scripting language that provides many convenient functions to complete these conversion operations. Below we will introduce how to realize the mutual conversion between timestamp and specific time in PHP.

Convert time into timestamp

PHP provides the time() function to obtain the current Unix timestamp. The Unix timestamp is from 0:00:00 on January 1, 1970 The number of seconds since the start of seconds (Greenwich Mean Time). If we want to convert local time to Unix timestamp, we can use the strtotime() function to achieve this.

// 获取当前Unix时间戳
$timestamp = time();

// 将当前时间转换为Unix时间戳
$timestamp = strtotime("now");

// 将指定的日期时间转换为Unix时间戳
$timestamp = strtotime("2022-06-28 09:20:00");

The strtotime() function can convert a time string into a Unix timestamp. It supports many time string formats, such as:

  • yyyy-MM-dd: Date string, such as "2022-06-28"
  • HH:mm:ss: Time string, such as "09:20:00"
  • yyyy-MM-dd HH:mm:ss: Date and time string , such as "2022-06-28 09:20:00"
  • now or 0 seconds: current time
  • 1 day or 1 week or 1 month or 1 year: Indicates adding 1 day/week/month/year
  • to the current time.

There are many other formats of strings that can be converted into Unix timestamps, which can be selected according to actual needs.

Convert timestamp to time

If we already have a Unix timestamp and want to convert it to a specific date and time, we can use the date() function. This function formats a datetime string according to the specified format.

// 获取当前时间戳
$timestamp = time();

// 将当前时间戳转换成指定格式的日期时间字符串
$datetime = date("Y-m-d H:i:s", $timestamp);

The following are some commonly used format characters:

  • Y: four-digit year, such as 2022
  • m: Two-digit month, such as 06
  • d: Two-digit date, such as 28
  • H: 24-hour hour, such as 09
  • i: Number of minutes, such as 20
  • s: Number of seconds, such as 00

and some others The format symbols can be selected according to specific needs. It should be noted that the second parameter of the date() function is a timestamp, so the timestamp needs to be converted before use.

Summary

PHP provides many simple functions that can easily realize the mutual conversion operation between timestamp and specific time. In actual development, it is necessary to select appropriate functions and formats according to actual needs as much as possible to achieve the best results.

The above is the detailed content of How to convert timestamps to each other 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