Home  >  Article  >  Backend Development  >  PHP date and time application 6: Check whether a date is a weekend

PHP date and time application 6: Check whether a date is a weekend

藏色散人
藏色散人Original
2021-08-03 10:09:582933browse

In the previous article "PHP date and time application five: Get the current date/time in different time zones and locations", I introduced you to the methods of obtaining time in different time zones. Next, I will continue to introduce you to the PHP date. The use of time, that is, how to determine whether a date is a weekend.

The theme of this article is to determine whether a date is a weekend, and weekend actually refers to Saturday to Sunday evening. After the implementation of the five-day work system, it refers to Saturday and Sunday every week. Presumably everyone already has an idea of ​​​​judgment.

Before introducing the method, let me share with you a little English knowledge~

Monday: Monday, the English abbreviation Mon.

Tuesday: Tuesday, English abbreviation Tue.

Wednesday: Wednesday, the English abbreviation Wed.

Thursday: Thursday, English abbreviation Thur.

Friday: Friday, the English abbreviation Fri.

Saturday: Saturday, the English abbreviation Sat.

Sunday: Sunday, the English abbreviation Sun.

Let’s go directly to the PHP code:

<?php
$dt=&#39;2021-08-03&#39;;
$dt1 = strtotime($dt);
$dt2 = date("l", $dt1);
$dt3 = strtolower($dt2);
if(($dt3 == "saturday" )|| ($dt3 == "sunday"))
{
    echo $dt3.&#39; 是周末&#39;."\n";
}
else
{
    echo $dt3.&#39; 不是周末&#39;."\n";
}

The running result is:

PHP date and time application 6: Check whether a date is a weekend

Obviously Tuesday is not the weekend~

Then let’s give a new date:

<?php
$dt=&#39;2021-08-08&#39;;
$dt1 = strtotime($dt);
$dt2 = date("l", $dt1);
$dt3 = strtolower($dt2);
if(($dt3 == "saturday" )|| ($dt3 == "sunday"))
{
    echo $dt3.&#39; 是周末&#39;."\n";
}
else
{
    echo $dt3.&#39; 不是周末&#39;."\n";
}

The result is:

PHP date and time application 6: Check whether a date is a weekend

Sunday is the weekend.

Isn’t it very simple~

A brief analysis of several key functions:

strtotime() Function: convert any English text The date or time description resolves to a Unix timestamp (number of seconds since January 1 1970 00:00:00 GMT).

strtolower()Function: Convert string to lowercase.

date()Function: Format local date and time and return the formatted date string.

Finally, I would like to recommend to you the latest free course on our platform "Entering the World of PHP from 0"~ Come and learn!

The above is the detailed content of PHP date and time application 6: Check whether a date is a weekend. 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