Search code examples
phpmysqlstrtotimerecurring

Specific date given only returns today


I am using this code found on Calendar Recurring/Repeating Events - Best Storage Method

Can someone please help me to let the script run from a given date? The script works fine, but it always start at today.

I want to change: $now = strtotime("today"); to $now = strtotime("2021-04-01");

//    $now = strtotime("today");
//$now = date( "Y-m-d", strtotime( "2009-01-31" ));
//$now = date("Y-m-d", strtotime("2014-12-31") );
//$now = "2013-04-11";
//$now = date("Y-m-d", strtotime("2015-04-01"))."\n";
//$now = strtotime('2007-12-21');
$now = strtotime('11-11-2007');
    $pushToFirst = 0;
    for($i = $pushToFirst; $i < $pushToFirst+3; $i++)
    {
        $now = strtotime("+".$i." day");
//$now = date("+".$i." day");
        $year = date("Y", $now);
        $month = date("m", $now);
        $day = date("d", $now);
        $nowString = $year . "-" . $month . "-" . $day;
        $week = (int) ((date('d', $now) - 1) / 7) + 1;
        $weekday = date("N", $now);
        echo $nowString . "<br />";
        echo $week . " " . $weekday . "<br />";
        $sql = "SELECT EV.*
                FROM `planering1` EV
                RIGHT JOIN `planering_namn` PN1 ON PN1.`NAMN` = EV.`namn`
                RIGHT JOIN `planering_meta` EM1 ON EM1.`event_id` = EV.`ID`
                WHERE ( DATEDIFF( '$nowString', repeat_start ) % repeat_interval = 0 )
                OR ( 
                    (repeat_year = $year OR repeat_year = '*' )
                    AND
                    (repeat_month = $month OR repeat_month = '*' )
                    AND
                    (repeat_day = $day OR repeat_day = '*' )
                    AND
                    (repeat_week = $week OR repeat_week = '*' )
                    AND
                    (repeat_weekday = $weekday OR repeat_weekday = '*' )
                    AND repeat_start <= DATE('$nowString')
                )";
        foreach ($dbConnect->query($sql) as $row) {
            print $row['namn'] . "\t";
            print $row['tur'] . "<br />";
        }
        echo "<br /><br /><br />";
    }

Solution

  • I would look at changing this part of your code

    // $now = strtotime('11-11-2007'); // remove this line
    $start = strtotime('2021-04-01'); // the date you want to start with
    
        $pushToFirst = 0;
        for($i = $pushToFirst; $i < $pushToFirst+3; $i++)
        {
            //        $now = strtotime("+".$i." day"); // remove this line
    
            // there are 86400 secs in a day, this will increase the day based on the start date, rather then just taking +$i days from now
            $now = $start + ($i * 86400); 
    

    Edit

    strtotime assumes you are talking about "now", as the default reference point. so if you say strtotime("+1 day"), that will be 1 day from now, and if you say strtotime("+5 days") that will be 5 days from now.

    There are a couple of ways around this,

    1. strtotime can accept 2 parameters

    https://www.php.net/manual/en/function.strtotime.php

    strtotime("+1 day", $start), will use $start at the reference point. so $start + 1day

    1. $now = $start + ($i * 86400); that is what i choose to use, each day has 86400 seconds, so i am just adding the number of days in seconds

    Both have the same outcome, use whatever one is more readable to you.