Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeat event on assigned day every week until specific date

When creating event I need to set event start and event end in datetime format (0000-00-00 00:00:00) Then I have option to set weekly repeating of that event until specific date (0000-00-00) But I can't insert repeating events properly in database. Here is what I have:

$startDateTime = '2015-04-30 10:30:00';
$endDateTime = '2015-04-30 11:30:00';
$repeatEndDate = '2015-06-01';
$timestamp = strtotime($startDateTime);
$day_of_week = date('l', $timestamp);
$step  = 1;
$unit  = 'W';
$repeatStart = new DateTime($startDateTime);
$repeatEnd   = new DateTime($repeatEndDate);
$repeatStart->modify($day_of_week);  
$interval = new DateInterval("P{$step}{$unit}");
$period   = new DatePeriod($repeatStart, $interval, $repeatEnd);

foreach ($period as $key => $date ) {
    $repeatQuery = 'INSERT INTO event(start,end,status,repeats) VALUES ("'.$startDateTime.'","'.$endDateTime.'","'.$status.'","'.$repeatEndDate.'")';
    $repeatResult = mysqli_query($db, $repeatQuery) or die (mysqli_error($db));
}

When I do print_r($date); it looks like this, no actual time just 00:00:00

DateTime Object
(
    [date] => 2015-04-30 00:00:00
    [timezone_type] => 3
    [timezone] => Europe/Berlin
)

I know that I can't insert values like that but I don't know how to get correct values from objects. So in this example I need to insert events that begin in 10:30:00 and end in 11:30:00(events always end same day) every Thursday starting at 2015-04-30 and ending at 2015-06-01. How can this be achieved?

Thank you

like image 617
Jakob Avatar asked Apr 30 '15 02:04

Jakob


People also ask

What does repeat weekly mean?

To repeat on certain days of the week (such as just Monday-Friday) use a weekly repeat - see below. Weekly Repeat. This handles events that repeat on a weekly basis and on possibly more than one day a week.

What is a recurring event?

A recurring event is an event that happens more than once, on a repeating schedule. When a repeating event is turned into individual event instances with individual dates, it is called “expanding” the event.


1 Answers

The problem is you're calling DateTime::modify with a day name (currently 'Thursday'). This value is calculated from the $startDateTime variable, and then used to modify that variable, so the only effect it has, is resetting the Time portion of that DateTime instance back to 0:00:00.

The following gives me the results I would expect: (I've commented out the parts you need to remove from yours, to make it easier to see the difference)

date_default_timezone_set('Etc/UTC');

$startDateTime = '2015-04-30 10:30:00';
$endDateTime = '2015-04-30 11:30:00';
$repeatEndDate = '2015-06-01';
#$timestamp = strtotime($startDateTime);
#$day_of_week = date('l', $timestamp);
$step  = 1;
$unit  = 'W';
$repeatStart = new DateTime($startDateTime);
$repeatEnd   = new DateTime($repeatEndDate);
#$repeatStart->modify($day_of_week);  
$interval = new DateInterval("P{$step}{$unit}");
$period   = new DatePeriod($repeatStart, $interval, $repeatEnd);



foreach ($period as $key => $date ) {
    echo($date->format('Y-m-d H:i:s')) . PHP_EOL;
}

The result from running the above is:

2015-04-30 10:30:00
2015-05-07 10:30:00
2015-05-14 10:30:00
2015-05-21 10:30:00
2015-05-28 10:30:00
like image 87
Stephen Avatar answered Oct 31 '22 20:10

Stephen