Home  >  Article  >  Backend Development  >  How to format timestamp to day of week in php

How to format timestamp to day of week in php

PHPz
PHPzOriginal
2023-03-29 16:16:501096browse

With the continuous development of Internet technology, the PHP language has become a very popular website development language. In the development of PHP, the application and processing of timestamps is also a very important link, and one of the common requirements is to convert timestamps into days of the week. This article will introduce how to convert timestamps and days of the week in PHP, and give corresponding code examples.

1. Timestamp

The timestamp refers to the number of seconds from 00:00:00 on January 1, 1970, Greenwich Mean Time to the present. Since this point in time is a great historical moment, it was chosen as the starting point of time in the computer. In PHP, you can use the time() function to get the current timestamp, as shown below:

$timestamp = time();
echo $timestamp; // 输出当前时间戳

For the timestamp of the specified time, you can use the strtotime() function to calculate, as shown below:

$timestamp = strtotime('2022-01-01 00:00:00');
echo $timestamp; // 输出指定时间的时间戳

2. Convert timestamp to date and day of the week

In PHP, you can use the date() function to convert the timestamp into a specified date format. The parameters of this function include two parts: format string and timestamp. Among them, the format string is used to define the output format of the date. Commonly used format characters are as follows:

Format characters Description
Y Year, four digits
m Month, two digits , with leading zeros
d Day, two digits, with leading zeros
H Hours, 24-hour clock, two digits
i Minutes, two digits, with leading zero
s Seconds, two digits, with leading zeros

For example, suppose there is a timestamp of 1642156800, which means 0:00 on January 14, 2022 Minutes and 0 seconds. If you want to convert it to the format of Friday, January 14, 2022, you can use the following code:

$timestamp = 1642156800;
$date = date('Y年m月d日 星期', $timestamp);
$weekday = date('N', $timestamp); //获取星期几
echo $date . $weekday; //输出形如2022年01月14日 星期五

Among them, in the date format string in the second line, the weekday character is uppercase The letter N means to output the numeric form of the day of the week (1 is Monday, 2 is Tuesday, and so on), and the $weekday variable in the third line obtains the day of the week number corresponding to the timestamp.

3. Convert the day of the week to a date

In addition to converting the timestamp to the day of the week, sometimes it is also necessary to convert the day of the week to the corresponding date. In PHP, you can also use the strtotime() function to achieve this conversion.

Specifically, you can first determine the day of the week corresponding to a certain date, and then calculate the corresponding date through descriptions such as "next Monday", "last Wednesday", etc. For example, the following code can obtain the Monday and Sunday of the week in which the current date is located:

$weekday = date('N'); // 获取当前星期几,数字形式
if ($weekday == 1) { // 如果是周一
  $monday = date('Y-m-d'); // 当天就是周一
} else { // 否则
  $monday = date('Y-m-d', strtotime('last Monday')); // 上周一
}
$sunday = date('Y-m-d', strtotime('next Sunday')); // 下周日
echo $monday . ' ~ ' . $sunday; // 输出本周一和周日日期范围

In the above code, the second line obtains the day of the week corresponding to the current date, and lines 4-7 determine whether the current day is Monday to determine whether Monday of this week is today or last week. The last line outputs the date range of this Monday and next Sunday.

4. Summary

This article introduces the related processing methods of timestamps, dates and days of the week in the PHP language, including obtaining the current timestamp, converting the timestamp to the specified date format and day of the week Day, and convert the day of the week to the corresponding date. We hope that the introduction in this article can help readers better understand and process timestamp and date data.

The above is the detailed content of How to format timestamp to day of week 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