Jump to content

[SOLVED] Problem With Date Code


UnsuitableBadger

Recommended Posts

Hi everyone,

 

I'm having trouble with my code:

 

<?php
    $startDate = date("2009-08-01");
    $endDate = date("2009-10-30");
    $todaysDate = date("Y-m-d");

    $recurrenceType = 1;
    $recurrenceNumber = 1;

    for ($loopDate = $startDate; $loopDate <= $endDate; $loopDate = date("Y-m-d", strtotime($loopDate)+86400))
    {
        $count++;
        // Daily
        if ($recurrenceType == 1)
        {
            if ($recurrenceNumber == 1)
                echo $loopDate."<br />";
            else
            {
                if (($count % $recurrenceNumber) == 1)
                {
                    echo $loopDate."<br />";
                }
            }
        }
        // Weekly
        elseif ($recurrenceType == 2)
        {
            if (($count % (7 * $recurrenceNumber)) == 1)
            {
                echo $loopDate."<br />";
            }
        }
        // Monthly
        elseif ($recurrenceType == 3)
        {
            if (($count % (30 * $recurrenceNumber)) == 1)
            {
                echo $loopDate."<br />";
            }
        }
    }
?>

 

This code basically runs from the $startDate to the $endDate and will list the dates as follows:

if $recurrenceType = 1 it will list dates daily

if $recurrenceType = 2 it will list dates weekly

if $recurrenceType = 3 it will list dates monthly

while $recurrenceNumber will list the recurring spacer so if you set $recurrenceType = 1 and $recurrenceNumber = 2 then the program will display every second date from start to end dates.

 

My problem is that if I start the date at 2009-08-01 and set the end date to 2009-10-25 or above it gets stuck on 2009-10-25 and just keeps spitting that date out.

 

Can anyone shed some light on why this would be happening?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/171606-solved-problem-with-date-code/
Share on other sites

The problem is where you are adding your extra day. It is giving the 1970 result.

 

Try this:

<?php
    $startDate = strtotime(date("2009-08-01"));
    $endDate = strtotime(date("2009-10-30"));
    $todaysDate = date("Y-m-d");

   $recurrenceType = 1;
    $recurrenceNumber = 1;

    for ($loopDate = $startDate; $loopDate <= $endDate; $loopDate = mktime(0,0,0,date("m",$loopDate),date("d",$loopDate)+1,date("Y",$loopDate)))
    {
        $count++;
        // Daily
        if ($recurrenceType == 1)
        {
            if ($recurrenceNumber == 1)
                echo date('Y-m-d',$loopDate)."<br />";
            else
            {
                if (($count % $recurrenceNumber) == 1)
                {
                    echo date('Y-m-d',$loopDate)."<br />";
                }
            }
        }
        // Weekly
        elseif ($recurrenceType == 2)
        {
            if (($count % (7 * $recurrenceNumber)) == 1)
            {
                echo date('Y-m-d',$loopDate)."<br />";
            }
        }
        // Monthly
        elseif ($recurrenceType == 3)
        {
            if (($count % (30 * $recurrenceNumber)) == 1)
            {
                echo date('Y-m-d',$loopDate)."<br />";
            }
        }
    }
?>

You're comparing dates which won't work. This works fine:

<?php
    $startDate = strtotime("2009-08-01");
    $endDate = strtotime("2009-10-30");
    $todaysDate = time();

    $recurrenceType = (isset($_GET['rt']))?$_GET['rt']:1;
    $recurrenceNumber = (isset($_GET['rn']))?$_GET['rn']:1;

    for ($loopDate = $startDate; $loopDate <= $endDate; $loopDate += 86400)
    {
        $count++;
        // Daily
        if ($recurrenceType == 1)
        {
            if ($recurrenceNumber == 1)
                echo date('Y-m-d',$loopDate)."<br />";
            else
            {
                if (($count % $recurrenceNumber) == 1)
                {
                    echo date('Y-m-d',$loopDate)."<br />";
                }
            }
        }
        // Weekly
        elseif ($recurrenceType == 2)
        {
            if (($count % (7 * $recurrenceNumber)) == 1)
            {
                echo date('Y-m-d',$loopDate)."<br />";
            }
        }
        // Monthly
        elseif ($recurrenceType == 3)
        {
            if (($count % (30 * $recurrenceNumber)) == 1)
            {
                echo date('Y-m-d',$loopDate)."<br />";
            }
        }
    }
?>

 

Ken

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.