Welcome! Log In Create A New Profile

Advanced

[PHP] detecting leap years?

Posted by Jeffry Killen 
Jeffry Killen
[PHP] detecting leap years?
January 20, 2018 02:40AM
Is there a function for detecting leap years?

I have developed a calendar generation module
that relies on javascript to let it know that a year
to be rendered in html markup is a leap year, so
it can add an extra day to February.

When the host html first loads, the module defaults
to the current year. So it doesn't get to the point that
javascript can decide and return its judgment.
I am using getDate to determine the current year.
I need to determine if a current year is a leap year.

I have written one for javascript, if there isn't a
built in function in php I can translate the js to php.

Thank you for time and attention;

Jeff k



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
Re: [PHP] detecting leap years?
January 20, 2018 02:40AM
On Fri, Jan 19, 2018 at 8:34 PM, Jeffry Killen <[email protected]> wrote:

> Is there a function for detecting leap years?
>
> I have developed a calendar generation module
> that relies on javascript to let it know that a year
> to be rendered in html markup is a leap year, so
> it can add an extra day to February.
>
> When the host html first loads, the module defaults
> to the current year. So it doesn't get to the point that
> javascript can decide and return its judgment.
> I am using getDate to determine the current year.
> I need to determine if a current year is a leap year.
>
> I have written one for javascript, if there isn't a
> built in function in php I can translate the js to php.
>
> Thank you for time and attention;
>
> Jeff k
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
function is_leap_year($year) {
return ((($year % 4) == 0) && ((($year % 100) != 0) || (($year % 400) == 0)));}

Source:https://davidwalsh.name/checking-for-leap-year-using-php
Jeffry Killen
Re: [PHP] detecting leap years?
January 20, 2018 02:50AM
> On Jan 19, 2018, at 5:36 PM, Aziz Saleh <[email protected]> wrote:
>
> function is_leap_year($year) {
>
>
> return ((($year % 4) == 0) && ((($year % 100) != 0) || (($year % 400) == 0)));
> }

Thank you;
Jeff k


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Richard
Re: [PHP] detecting leap years?
January 20, 2018 02:50AM
> Date: Friday, January 19, 2018 20:36:51 -0500
> From: Aziz Saleh <[email protected]>
>
> On Fri, Jan 19, 2018 at 8:34 PM, Jeffry Killen
> <[email protected]> wrote:
>
>> Is there a function for detecting leap years?
>>
>> I have developed a calendar generation module
>> that relies on javascript to let it know that a year
>> to be rendered in html markup is a leap year, so
>> it can add an extra day to February.
>>
>> When the host html first loads, the module defaults
>> to the current year. So it doesn't get to the point that
>> javascript can decide and return its judgment.
>> I am using getDate to determine the current year.
>> I need to determine if a current year is a leap year.
>>
>> I have written one for javascript, if there isn't a
>> built in function in php I can translate the js to php.
>>
>
> function is_leap_year($year) {
> return ((($year % 4) == 0) && ((($year % 100) != 0) || (($year %
> 400) == 0)));}
>
> Source:https://davidwalsh.name/checking-for-leap-year-using-php


Or the "L" parameter:

L Whether it's a leap year -- 1 if it is a leap year, 0 otherwise.

on the date() function:

http://php.net/manual/en/function.date.php




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jeffry Killen
Re: [PHP] detecting leap years?
January 20, 2018 08:40AM
> On Jan 19, 2018, at 5:42 PM, Jeffry Killen <[email protected]> wrote:
>
>
>> On Jan 19, 2018, at 5:36 PM, Aziz Saleh <[email protected]> wrote:
>>
>> function is_leap_year($year) {
>>
>>
>> return ((($year % 4) == 0) && ((($year % 100) != 0) || (($year % 400) == 0)));
>> }
>
> Thank you;
> Jeff k
>

Thank you Richard for

Or the "L" parameter:

L Whether it's a leap year -- 1 if it is a leap year, 0 otherwise.

on the date() function:

This is helpful to know in the future. Meanwhile,

I modified the original code in reply to the following to work in my situation

1: I couldn't seem to get a decisive response from code as posted in reply

were 2018 is from getDate return array
print is_leap_year(2018) // ... nothing, no false, -1, 0 or anything. also no complaints

2: I have had problems with using % alone in php, and reading the manual
it didn't actually do what it does in javascript: 2.5%1 = .5, so I used fmod
I wrote a bunch if code in javascript and did an almost verbatim translation
to php. But every where I used % in the javascript code, I had to use fmod
in php to get the same result.

3: If the $year value comes in as a string so I didn't want to trust that it would
be coerced to number type.

4: The return doesn't have to be a string, it is a left over from processing
string representation of boolean value, as would be sent in a $_GET var.

function getLeap($year)
{
settype($year, 'int');
if( ( fmod($year, 4) == 0 ) && ( ( fmod($year, 100) != 0 ) || (fmod($year, 400) == 0) ) )
{
return 'true';
}
else
{
return 'false';
}
}

I hope this considered something of value to list in general. It is not intended as a criticism
of the code offered. I was probably missing some key understanding.

Jeff K



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Per Jessen
Re: [PHP] detecting leap years?
January 22, 2018 08:20AM
Jeffry Killen wrote:

>
> 2: I have had problems with using % alone in php, and reading the
> manual
> it didn't actually do what it does in javascript: 2.5%1 = .5, so I
> used fmod
> I wrote a bunch if code in javascript and did an almost verbatim
> translation to php. But every where I used % in the javascript
> code, I had to use fmod in php to get the same result.

In php, '%' is integer modulus, it's quite clearly documented.



--
Per Jessen, Zürich (7.0°C)
http://www.dns24.ch/ - your free DNS host, made in Switzerland.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Michelle Konzack
Re: [PHP] detecting leap years?
January 22, 2018 08:40AM
Am 2018-01-20 hackte Jeffry Killen in die Tasten:
> Is there a function for detecting leap years?

As far as I know no, but I asked date() for the (day of year)
by feeding date() with "$current_year-12-31"
which should give you 365 or 366.


Thanks in avance

--
Michelle Konzack Miila ITSystems @ TDnet
GNU/Linux Developer 00372-54541400


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login