First of all, I've searched through similar topics and I couldn't find an answer to my issue. So I have an event manager with reoccurring events. I want to store the starting date of every instance of a repeating event up to the ongoing one.
Goal:
array (size=5)
0 =>
object(DateTime)[288]
public 'date' => string '2015-08-11 14:30:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
1 =>
object(DateTime)[288]
public 'date' => string '2015-08-18 14:30:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
2 =>
object(DateTime)[288]
public 'date' => string '2015-08-25 14:30:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
3 =>
object(DateTime)[288]
public 'date' => string '2015-09-01 14:30:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
What I get:
array (size=5)
0 =>
object(DateTime)[288]
public 'date' => string '2015-09-01 14:30:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
1 =>
object(DateTime)[288]
public 'date' => string '2015-09-01 14:30:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
2 =>
object(DateTime)[288]
public 'date' => string '2015-09-01 14:30:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
3 =>
object(DateTime)[288]
public 'date' => string '2015-09-01 14:30:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
In short, all previous values in the array get overwritten by the last assigned value as the code runs through the loop.
Code:
class Model_Event {
private $post_id;
private $count;
private $instances;
public function __construct( $post_id ) {
$this->post_id = $post_id;
$this->count = (int) 0;
$this->instances = array();
$this->fetch_instances();
}
private function fetch_instances() {
// Abort if no starting date defined.
if ( !$this->has_date() )
return;
// Set the original instance.
$this->instances[ $this->count ] = $this->datetime( 'both', 'from', false, true );
// Abort if event not repeating.
if ( !$this->is_repeating() )
return;
while ( !$this->is_stop_reached( $this->instances[ $this->count ], $this->count ) && $this->is_instance_started( $this->instances[ $this->count ] ) ) :
$this->instances[ $this->count + 1 ] = $this->next_instance( $this->instances[ $this->count ] );
$this->count++;
endwhile;
var_dump($this->instances);
return;
}
private function next_instance( DateTime $dateObject ) {
if ( $this->repeat_unit() === 'h' )
return $this->add_hours( $this->repeat_value(), $dateObject );
if ( $this->repeat_unit() === 'd' )
return $this->add_days( $this->repeat_value(), $dateObject );
if ( $this->repeat_unit() === 'w' )
return $this->add_weeks( $this->repeat_value(), $dateObject );
if ( $this->repeat_unit() === 'm' )
return $this->add_months( $this->repeat_value(), $dateObject );
if ( $this->repeat_unit() === 'y' )
return $this->add_years( $this->repeat_value(), $dateObject );
}
/**
* Currently only testing on "repeat every X weeks".
*/
private function add_weeks( $weeks, DateTime $dateObject ) {
return $dateObject->modify('+'.$weeks.' week');
}
...
}
I am trying to fix this for over 5 hours and can't seem to wrap my head around the problem. Thank you.
goal
andWhat I get:
snippets – J Santosh Sep 5 at 8:21$this->instances[ $this->count + 1 ] = $this->next_instance( $this->instances[ $this->count ] );
– Filip Juncu Sep 5 at 8:40