Home  >  Article  >  Backend Development  >  How to batch convert timestamps in php

How to batch convert timestamps in php

PHPz
PHPzOriginal
2023-03-29 11:34:481166browse

When writing PHP code, you often need to deal with time-related matters. Date times are often stored and transmitted in the form of timestamps, so timestamps need to be converted in batches across different applications and systems. This article will introduce how to implement batch conversion of timestamps in PHP.

  1. Definition of timestamp

Time stamp is a way of representing date and time in a computer system. It is a number that represents the number of seconds that have elapsed since 0:00:00 GMT on January 1, 1970. For example, a timestamp of 1533145278 means the time since 0:00:00 on January 1, 1970, after 1533145278 seconds have passed.

  1. Conversion of timestamps

In PHP, you can use the date() and strtotime() functions to convert timestamps and date times to and from each other.

2.1 date() function

The date() function can convert timestamps into specific date and time formats. Its syntax is as follows:

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

Among them, the $format parameter is the format string of date and time, $timestamp Is an optional timestamp. If no timestamp is provided, the default is the current system time.

For example, the following code converts timestamp 1533145278 to the format of "2018-08-01 09:14:38":

$timestamp = 1533145278;
$datetime = date('Y-m-d H:i:s', $timestamp);
echo $datetime; //输出:2018-08-01 09:14:38

2.2 strtotime() function

The strtotime() function can convert a datetime format string into a corresponding timestamp. Its syntax is as follows:

int strtotime ( string $time [, int $now = time() ] )

Among them, the $time parameter is the format string of date and time, $now Is an optional timestamp. If no timestamp is provided, the default is the current system time.

For example, the following code converts the date and time string "2018-08-01 09:14:38" to timestamp 1533145278:

$datetime = '2018-08-01 09:14:38';
$timestamp = strtotime($datetime);
echo $timestamp; //输出:1533145278
  1. Batch conversion of timestamps

In actual development, we may need to convert multiple timestamps in an array to a specific date and time format, or convert multiple date and time strings in an array to timestamps. In order to achieve batch conversion, we can use PHP's array traversal function foreach() to traverse the array elements, and then convert the timestamp and date time for each element.

3.1 Batch conversion of timestamps to date and time

The following code demonstrates how to batch convert a timestamp array to date and time format:

$timestamps = [1533145278, 1553528551, 1585061157];
$datetimes = [];

foreach ($timestamps as $timestamp) {
    $datetime = date('Y-m-d H:i:s', $timestamp);
    $datetimes[] = $datetime;
}

print_r($datetimes);

The output is as follows:

Array
(
    [0] => 2018-08-01 09:14:38
    [1] => 2019-03-25 15:15:51
    [2] => 2020-03-24 01:52:37
)

3.2 Batch conversion of date and time into timestamps

The following code demonstrates how to batch convert an array of date and time strings into timestamps:

$datetimes = ['2021-01-01 00:00:00', '2022-02-02 02:02:02', '2023-03-03 03:03:03'];
$timestamps = [];

foreach ($datetimes as $datetime) {
    $timestamp = strtotime($datetime);
    $timestamps[] = $timestamp;
}

print_r($timestamps);

The output is as follows:

Array
(
    [0] => 1609459200
    [1] => 1643764922
    [2] => 1675260183
)
  1. Summary

This article introduces how to implement batch conversion of timestamps in PHP. By using the date() and strtotime() functions, we can easily convert timestamps and date times to each other, and through the array traversal function foreach(), we can also achieve batch conversion of multiple timestamps or date times. These techniques are often used in actual development, and I hope they will be helpful to beginners and PHP enthusiasts.

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