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='2021-08-03'; $dt1 = strtotime($dt); $dt2 = date("l", $dt1); $dt3 = strtolower($dt2); if(($dt3 == "saturday" )|| ($dt3 == "sunday")) { echo $dt3.' 是周末'."\n"; } else { echo $dt3.' 不是周末'."\n"; }
The running result is:
Obviously Tuesday is not the weekend~
Then let’s give a new date:
<?php $dt='2021-08-08'; $dt1 = strtotime($dt); $dt2 = date("l", $dt1); $dt3 = strtolower($dt2); if(($dt3 == "saturday" )|| ($dt3 == "sunday")) { echo $dt3.' 是周末'."\n"; } else { echo $dt3.' 不是周末'."\n"; }
The result is:
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!