Home > Backend Development > PHP Problem > How to determine the day of the week based on timestamp in php

How to determine the day of the week based on timestamp in php

青灯夜游
Release: 2023-03-13 06:26:02
Original
2723 people have browsed it

In PHP, you can use the date() function to determine the day of the week based on the timestamp. This function can format the timestamp into a readable date and time string; the syntax format is "date("w ",timestamp);", "w" specifies that the timestamp be formatted as a number representing the day of the week, 0 represents Sunday, 6 represents Saturday.

How to determine the day of the week based on timestamp in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In PHP, you can date( ) function to determine the day of the week based on the timestamp.

date() function formats the local date and time and returns the formatted date string.

Syntax:

date(format,timestamp);
Copy after login

When format is set to "w", you can specify that the timestamp is formatted as a number representing the day of the week, (0 represents Sunday [Sunday], 6 represents Saturday [Saturday] ]).

<?php
header("Content-type:text/html;charset=utf-8");
$time=strtotime("2021-09-25");
$week = date("w",$time);
echo $week;
?>
Copy after login

Output:

6
Copy after login

Let’s optimize the code:

<?php
header("Content-type:text/html;charset=utf-8");
$weekarray = array("日","一", "二", "三", "四", "五", "六");
$time=strtotime("2021-09-26");
$week = $weekarray[date("w",$time)];
echo "周".$week."<br>";

$time=strtotime("2021-09-25");
$week = $weekarray[date("w",$time)];
echo "周".$week;
?>
Copy after login

Output:

How to determine the day of the week based on timestamp in php

Recommended learning : "PHP Video Tutorial"

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template