How to get the date and week of the last few days in php

PHPz
Release: 2023-03-31 09:35:33
Original
1114 people have browsed it

In PHP, getting the date and week of the last few days is not a complicated task and can be easily accomplished using the date function provided by PHP. This article will introduce how to use the date and strtotime functions to get the date and week of the last few days.

1. Get the current date and weekday

Getting the current date and weekday is the simplest task, just call the date function. Here is the code to get the current date and week:

$date = date('Y年m月d日');
$week = date('l');
echo $date . ' ' . $week;
Copy after login

In the above code, the first parameter of the date function is the date format, which can be changed as needed. The format for getting the day of the week is 'l', which means the complete name of the week (such as Monday, Tuesday, etc.), you can also use 'D' (week abbreviation, such as Mon, Tue, etc.) or 'N' (week of the week in ISO-8601 format numbers, 1 for Monday, 7 for Sunday) instead.

2. Get the date and week of the last few days

If you want to get the date and week of the last few days, you can use the strtotime function to generate a date range, and then combine it with the date function to get the date and week. The following is the code to get the date and week of the last 7 days:

$today = date('Y-m-d');
$weekdays = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

for ($i = 1; $i <= 7; $i++) {
    $day = strtotime(&#39;-&#39; . $i . &#39; days&#39;, strtotime($today));
    $date = date(&#39;Y年m月d日&#39;, $day);
    $week = $weekdays[date(&#39;w&#39;, $day)];
    echo $date . &#39; &#39; . $week . &#39;<br />';
}
Copy after login

In the above code, first get the current date and save it in the $today variable, and then define an array $weekdays containing the name of the week, Then use a for loop to get the dates of the last seven days. In the loop body, use the strtotime function to generate a date based on the current date and the number of loops, and then use the date function to format the date and day of the week.

3. Notes

There are some details that need to be noted in the above code:

  • The date function will use the server’s local time zone by default. If you need to use other Time zone, you need to set the time zone parameters. For example: date_default_timezone_set('Asia/Shanghai');
  • strtotime function requires two parameters, the first parameter is the time string format, and the second parameter is the timestamp. The return value is a Unix timestamp representing date/time;
  • When using the date function, you need to pay attention to giving the correct format string, otherwise the returned result may not be what you expect.

4. Summary

This article introduces how to use PHP's date and strtotime functions to obtain the current date and week and the date and week of the last few days. These functions are commonly used tools if you need to handle date and time related tasks. Once you master the basic usage of these functions, you can easily implement various date and time-related logic.

The above is the detailed content of How to get the date and week of the last few days in php. For more information, please follow other related articles on the PHP Chinese website!

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