Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Kevin Korte
Kevin Korte
28,109 Points

Quick Easy Question about PHP's date function and leap years

Hello,

I have a questions I think should be easy for somebody who has used PHP's date function.

I know that

date( 'z' );

calculates how many days into the year we are. PHP.net says this returns a value between 0 and 365. Perfect.

However, leap years have 366 days. I know that

 date( 'L' )

will be zero if it's not a leap year, and one if it is.

My question is will date( 'z' ) count up to 366 days on a leap year, or will I have to add one day on leap years? If I do that, will date( 'z') roll back to 0 on Dec. 31 of a leap year anyway because it can't count to 366?

Can I also assume that March 1st on a non-leap year will be 59, and March 1st on a leap year be equal to 60?

Yep, not sure how date( 'z' ) deals with counting that extra day in February, and not sure how to test it either.

Here is the doc page I'm using: http://php.net/manual/en/function.date.php

Thanks!

Kevin Korte
Kevin Korte
28,109 Points

Just figured out how to use mktime() so that is helping troubleshoot this.

All figured out that because Jan 1 is 0, that dec 31 is 364 on non leap years and 365 on leap years. So one thing down.

2 Answers

Kevin Korte
Kevin Korte
28,109 Points

Okay, I think I found my solution. This seems like a reliable way to tell if it's easter day regardless of the year. Let me know. I'm a PHP rookie still.

Gets today's date.
$date = time();

Gets the current year.
$year = date( 'Y', $date );

Gets number of additional days after March 21 to Easter day.
$easter = easter_days( $year );

Calculates how many days in March 21st is. Accounts for leap years.
$daysIntoMarch = date( 'z', mktime( 0, 0, 0, 3, 21, $year));

Add how many days to March 21st, plus how many days after 3/21 to get how many days into year easter is
$thisEaster = $easter + $daysIntoMarch;

Logic to compare how many days we are into this year, and if it must be Easter day.
if (date( 'z' ) == $thisEaster) {
    echo "We're Closed. Happy Easter";
}

There is more to this code for other holidays, but does this seem like a reasonable and pretty solid approach to tell if it's Easter day or not, taking into account leap years?

Kevin Korte
Kevin Korte
28,109 Points

Sorry to bug Randy Hoyt or Zac Gordon but since you guys do the majority of the PHP stuff here, can you read through this code? I'm stepping out of my comfort zone here writing this code. Thanks so much!