Home >Backend Development >PHP Problem >How to determine the day of the week based on timestamp in php
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.
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:
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!