Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

php - Checking if there's a leap day in a timerange

How to check if there is a February 29th in between 2 timestamps?

$date_from = '2007-06-01';
$date_to = '2013-05-30';

I know in this range there's 2 times a feb-29, but how do I check for it?

I want to count the days in between, but leap days don't count.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This will loop through the years, but it is even easier after you find a leap year because the next one will be 4 years after, etc.

$date_from = strtotime('2007-06-01');
$date_to = strtotime('2013-05-30');

for($year=$date_from; $year<=$date_to; $year=strtotime('next year', $year)) {
    echo date('Y', $year);
    echo date('L', $year) ? 'Leap year' : 'Not leap year';
}

But I think you can just check if the year 'Y' is evenly divisible by 4.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...