Jump to content

[SOLVED] Date() with Leap Year Question


phpQuestioner

Recommended Posts

ok so; let me give you an example

 

<?php
$doy = date(("z"), time() - (14400 * 1));
$calculate = 365 - $doy;
$currentyear = date(("Y"), time() - (14400 * 1));
$output = $currentyear + 1;

if ($calculate == 0)
{
echo "Happy New Year";
}
else if ($calculate <= 365)
{
echo "There are $calculate days left until $output.";
}
?>

 

Now I know I probably could set up code to check for leap year. But the question now posed, is will leap year automatically be configured into the 365 or will I have to create some code too look for leap year; that will add 365 + 1 (or something like that), if leap year occurs?

Thanks for the help dustinnoe and kenrbnsn; I came up with a little bit of a different solution.

 

 

<?php

$check4leap = date(("L"), time() - (14400 * 1));

if ($check4leap == 1) {
$daysNyear="366";
}
else {
$daysNyear="365";
}

$doy = date(("z"), time() - (14400 * 1));
$calculate = $daysNyear - $doy;
$currentyear = date(("Y"), time() - (14400 * 1));
$output = $currentyear + 1;

if ($calculate == 0)
{
echo "Happy New Year";
}
else if ($calculate <= $daysNyear)
{
echo "There are $calculate days left until $output.";
}
?>

Here is a more simplified solution

 

<?php
$doy = date(("z"), time() - (14400 * 1));
$currentYear = date(("Y"), time() - (14400 * 1));
$lastDay = strtotime("Dec 31, $currentYear");
$numDays  = date('z', $lastDay);
$daysTil = $numDays - $doy;

if ($doy == 0)
{
echo "Happy New Year!";
}
else
{
$newYear = $currentYear + 1;
echo "There are {$daysTil} days left until {$newYear}.";
}
?>

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.