Home  >  Article  >  Backend Development  >  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

青灯夜游
青灯夜游Original
2021-09-26 16:44:582657browse

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);

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;
?>

Output:

6

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;
?>

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!

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