
下面是"Baidu百科"关于公历“闰年”词条的算法说明
“闰年是公历中的名词。
普通年能被4整除且不能被100整除的为闰年。(如2004年就是闰年,1900年不是闰年);
世纪年能被400整除的是闰年。(如2000年是闰年,1900年不是闰年);
对于数值很大的年份,这年如果能被3200整除,并且能被172800整除则是闰年。如172800年是闰年,86400年不是闰年(因为虽然能被3200整除,但不能被172800整除)。
闰年(Leap Year)是为了弥补因人为历法规定造成的年度天数与地球实际公转周期的时间差而设立的。补上时间差的年份为闰年。闰年共有366天(1-12月分别为31天,29天,31天,30天,31天,30天,31天,31天,30天,31天,30天,31天)。”
根据上面算法用MySQL存储过程判断某个年份是否为闰年,请参考下列实验(注意:由于MySQL时间日期类型的年份取值范围为1000~9999,因此对于上述关于大数值年份的算法予以忽略)
-- 创建自定义存储函数”Is_Leap_Year“,用来判定某个年份是否为公历闰年
Drop Function if exists Is_Leap_Year
CREATE FUNCTION Is_Leap_Year (dt date) RETURNS Bit(1)
BEGIN
Declare result Bit
If Mod(Year(dt),400)=0 Or
(Mod(Year(dt),4)=0 And Mod(Year(dt),100)<>0) Then
Set result=1
Else
Set result=0
End If
RETURN result
END
-- 创建测试表并插入数据
Drop Table If Exists test
Create Table test
(id int auto_increment primary key,d1 DateTime)
Insert into test (d1) values
('2010-05-26'),('2011-06-16'),
('2012-07-23'),('2013-08-02'),
('2014-09-20'),('2015-10-27'),
('2016-11-24'),('2017-12-13')
-- 测试自定义函数Is_Leap_Year返回结果
select *,
AddDate(concat(year(d1),'-03-01'),-1)
as Last_Date_Feb_of_The_Year,
If(Is_Leap_Year(d1),'是','否')
as `Is_Leap_Year(d1)`
from test
实验截图
用函数date_sub或date_add直接对"年"部分去减就可以了,见下面示例:mysql>select now()
+---------------------+
| now() |
+---------------------+
| 2010-08-25 09:34:41 |
+---------------------+
1 row in set (0.00 sec)
mysql>select date_add(now(),interval -1 year)
+----------------------------------+
| date_add(now(),interval -1 year) |
+----------------------------------+
| 2009-08-25 09:34:44 |
+----------------------------------+
1 row in set (0.00 sec)
mysql>select date_sub(now(),interval 1 year)
+---------------------------------+
| date_sub(now(),interval 1 year) |
+---------------------------------+
| 2009-08-25 09:34:46 |
+---------------------------------+
1 row in set (0.00 sec)
mysql>
1、首先在电脑上创建一个index.php文件,编辑index.php。
2、然后输入获取当天零点的时间戳,输入代码$today = strtotime(date("Y-m-d"),time())$time = strtotime($today)//获取到echo $time."<br />"//输出。
3、获取当天24点的时间戳$todayEnd = $today+60*60*24//家一天的时间echo $time = strtotime($todayEnd)."<br />"//输出。
4、获取前一天时间echo date("Y-m-d H:i:s",strtotime("-1 days"))。
5、以上即时间戳和前一天时间的获取。主要是对date()和strtotime()函数的灵活使用,就可以了。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)