Repetition of monthly and annual events - how to ensure accuracy?

I currently allow daily or weekly recurring events in the calendar application (using fullCalendar).

My view has a checkbox that activates two drop-down lists: one for the repetition interval (daily, weekly) and one for the frequency (once, twice, etc.).

$evt_interval_options = array( //this will be $row->interval in the model
    '86400' => 'Daily',   
    '604800' => 'Weekly'; 

$evt_frequency_options = array(  //this will be //$i in the model
    '1' => '2',    
    '2' => '3',    

<?php echo form_checkbox('evt_repeat', 'FALSE', FALSE, 'class="repeat_checkbox"'); 
      echo form_dropdown('evt_interval', $evt_interval_options');
      echo form_dropdown('evt_frequency', $evt_frequency_options'); ?>

This ultimately reaches my model, which starts the loop, checking if the event should be repeated - if so, it will depend on the interval ( $row->interval) and frequency ( $i).

$cal_data[] = array(
    'start' => strtotime($row->date_1 . ' ' . $row->time_1) + ($i ? ($row->interval * $i) : 0),
);

This works well to display multiple daily or weekly events based on one entry in the database.

. ,

03/01/2011 00:00:00 --> 04/01/2011 00:00:00 ===> 2674800 seconds
04/01/2011 00:00:00 --> 05/01/2011 00:00:00 ===> 2592000 seconds
and so on for monthly and yearly differences

, ? - strtotime, , . 4th July 4th?

PHP 5.2.14.

+3
4

, - @akniep PHP.net strtotime ( ).

function get_x_months_to_the_future( $base_time = null, $months = 1 )
{
    if (is_null($base_time))
        $base_time = time();

    $x_months_to_the_future    = strtotime( "+" . $months . " months", $base_time );

    $month_before              = (int) date( "m", $base_time ) + 12 * (int) date( "Y", $base_time );
    $month_after               = (int) date( "m", $x_months_to_the_future ) + 12 * (int) date( "Y", $x_months_to_the_future );

    if ($month_after > $months + $month_before)
        $x_months_to_the_future = strtotime( date("Ym01His", $x_months_to_the_future) . " -1 day" );

    return $x_months_to_the_future;
}

, 29, 30, 31, Feb 28.

, POV .

, Jan 31 normal strtotime, , 31 .

, Google Calendar , , .

, , - Jan 31 Feb 28 Mar 31 Apr 30 ..

, , , . , 12 .

+5

, DateTime PHP

$dt = new DateTime('3rd Jan 2011');
echo $dt->format('r') . PHP_EOL;

$dt->modify('+1 year');
echo $dt->format('r') . PHP_EOL;

edit: , , 29,30,31 , . :

$dt = new DateTime('29 Jan 2011');
echo $dt->format('r') . PHP_EOL;

$dt->modify('+1 month');
echo $dt->format('r') . PHP_EOL;
+3
$evt_interval_options = array( //this will be $row->interval in the model
    '86400' => 'Daily',   
    '604800' => 'Weekly';

, . DST-, , . , , .

PHP DateTime / strtotime. : strtotime('seconds monday of next month'); 13 .

+2

strtotime , " " "2011-05-09 ". . , . "2011-01-30 ", 30 , 1/2 ( ).

, y/m/d/h/m/s, , mktime(), . , " ", , .

+1
source

All Articles