Jump to content

[SOLVED] time/date stamp and leap year


noname_clark

Recommended Posts

so i am creating this calendar for my website, and i need to know if I can figure out if it a leap year for a specific year.

 

so for example, the url is: calendar.php?month=February&year=2008

 

how can i have it so that when it goes to that page, it knows if it is a leap year based on the 'year' variable in the query string?

 

or maybe enen a little simpler example:

how can i have the page know if calendar.php?month=February&year=2008 is a leap year calendar verses

calendar.php?month=February&year=2020

 

???  any help would be great. thanks.

Link to comment
https://forums.phpfreaks.com/topic/127085-solved-timedate-stamp-and-leap-year/
Share on other sites

$time = strtotime('1/1/2008');
echo date('L', $time);

 

1 for leap year, 0 for not.

 

Much easier than doing the math yourself :) there are so many weird rules for whether something is a leap year or not.

 

edit: woops, forgot the day/month. should work fine now.

 

meh, why not add the actual algorithm?

 

<?php
function isLeapYear($year) {
return ($year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0));
}
?>

 

use that if for some reason you have a problem with the date() one.

thanks for replying =)

 

so i tried the:

 

$time = strtotime('1/1/2008');

echo date('L', $time);

 

but i had to put $year in for '2008' so that i could change the year based on which year the user wanted to see, so i tried this:

 

$time = strtotime('1/1/$year');

echo date('L', $time);

 

now the value of $time is -1

where if it was left at '2008' i get 1199163600

 

so how do i pass the varable $year using strtotime?

 

or, using the algorithm how does it work so i can try that? (sorry, i dont know much about functions in php)

in php if you use single quotes it doesn't parse for variables so you'd need to do

 

"1/1/$year" or '1/1/'.$year

 

As for the function you would call it

 

isLeapYear($year);

 

it makes sure the year is divisible by 4, but not by 100 unless it is divisible by 400.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.