Summary of php date operation skills

怪我咯
Release: 2023-03-13 15:24:01
Original
1100 people have browsed it

This article mainly introduces the php date operation skills, involving the display format conversion skills of PHP date and time, which has certain reference value. Friends in need can refer to this article

The example summarizes the PHP date operation skills. Share it with everyone for your reference, the details are as follows:

1. PHP converts the date format obtained in the form into a unified format

2017-7-9 All unified Convert to 2015-09-09 so that the database has a unified format, which is convenient for future queries

$year = "2017";
$month = "7";
$day = "09";
var_dump(checkdate($month,$day, $year));//月和日带有前导0都是符合格式的
if(checkdate($month,$day, $year)===false){
  exit('error');
};
$unixtime = mktime(2,2,2,$month,$day,$year);//目的是交给php转换成月和日都带有前导0的格式统一的格式存储在数据库方便以后查询
var_dump(date("Y-m-d",$unixtime));
////交给php转换成时间戳,然后反转回来
Copy after login

2. Get the start timestamp and end timestamp of the previous day

The original idea is:

First use date to get the year, month and day of the day. Get it separately. The year is 2015, the month is 9, and the day is 28
, and then subtract 1. But a problem arises.

What if today is the 1st. Subtract 1 and it becomes 0. The last month could be 28 days or it could be 30 days.

In this way, first get the timestamp of the previous day. Let php automatically calculate it.

strtotime("-1 day");
//得到上一天的时间戳,现在是几点就得到上一天这个时间点的时间戳,用这种方式好处是解决了上面问题,php会自动去计算上个月多少天
Copy after login
rrree

The above is the detailed content of Summary of php date operation skills. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!