Home  >  Article  >  Backend Development  >  How to convert specified date to week in php

How to convert specified date to week in php

青灯夜游
青灯夜游Original
2022-05-12 21:06:162972browse

Conversion method: 1. Use strtotime() to convert the specified date into a timestamp, the syntax is "strtotime("date")"; 2. Use date() with the formatting character "N" to convert the timestamp As a number representing the day of the week, the syntax "date("N", timestamp)" will return 1 (Monday) to 7 (Sunday).

How to convert specified date to week in php

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

php will specify the date Method of converting to week

In PHP, you can use the date() and strtotime() functions for conversion.

(In fact, date() is mainly used, but the object processed by this function is a timestamp; therefore we need to use the strtotime() function to convert the date to a timestamp first, and then hand it over to date() for processing.)

1. Use the strtotime() function to convert the specified date into a timestamp

strtotime() function can parse the date and time description described in English text into a UNIX timestamp

<?php
header("Content-type:text/html;charset=utf-8");
$str1="2022-05-1";
$str2="2022-05-11";
$time1=strtotime($str1);
$time2=strtotime($str2);
echo "$str1 的时间戳: ".$time1."<br>";
echo "$str2 的时间戳: ".$time2;
?>

How to convert specified date to week in php

2. Convert the obtained timestamp to a Sunday number

date() can format the time and match the formatting characters "N" gets the Sunday number

  • N: Returns the day of the week represented by numbers in ISO-8601 format (newly added in PHP 5.1.0), range 1 (indicating Monday) Check your calendar by 7 (meaning Sunday)

echo "$str1 是星期 ".date("N",$time1)."<br>";
echo "$str2 是星期 ".date("N",$time2);

How to convert specified date to week in php

to see if it is correct.

How to convert specified date to week in php

Recommended: "PHP Video Tutorial"

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