Идея была не короткого кода, а показать как можно реализовать:
Создаётся новое событие.
Для этого - создаётся файл с наследованием одного интерфейса и подключается. Колонка добавляется сама.
Решение я делал как разбитие логики.
- Кода много и файлов.,
Решение которые приложили здесь на форуме, мне нравятся и они будут работать.
Мне просто интересно кто как бы сделал и мыслит , особенно когда нету условий по времени и памяти.
:lol:
В след раз, я создам топик, задачу на время и скорость. :)
Index.php<?php
error_reporting(-1);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
require ('Event.php');
require ('events/IEvent.php');
require ('events/Meeting.php');
require ('events/Testing.php');
$event = new Events(6);
$event->setEvent(new \Event\Meeting());
$event->setEvent(new \Event\Testing());
$event->run();
echo 'DONE';
Events.php<?php
class Events {
private $events = array();
private $period = 5;
private $data = array();
private $columns = array('month' => array(
'Month'
));
private $filename = 'data.csv';
public function __construct($period = 5) {
$this->period = $period;
}
public function setEvent(\Event\IEvent $event) {
$this->events[] = $event;
}
public function run() {
if(!empty($this->events)) {
$total = 0;
foreach ($this->events as $eventName => $event) {
$this->data[$eventName] = array($event->getTitle());
for($i = 1; $i <= $this->period ; $i++){
if($i != 1) {
$event->setDate('next month');
}
if($total === 0) {
$this->columns['month'][] = $event->getDate()->format('M');
}
$this->data[$eventName][] = $event->getDate()->format('Y-m-d D');
}
}
}
$this->save();
}
private function save() {
$fp = fopen($this->filename, 'w');
for($i = 0; $i < $this->period+1; $i++) {
$fields[] = $this->columns['month'][$i];
for($f = 0; $f < count($this->data); $f++) {
$fields[] = $this->data[$f][$i];
}
fputcsv($fp,$fields);
$fields = array();
}
fclose($fp);
}
}
events folder:
IEvent.php<?php
namespace Event;
interface IEvent {
public function getDate() ;
public function setDate($modify);
public function getTitle();
}
Meeting.php<?php
namespace Event;
class Meeting implements IEvent {
public $title = 'Mid Month Meeting Date';
private $day = 14;
private $exceptions = array('Sun','Sat');
private $date;
public function __construct() {
$this->date = new \DateTime();
}
public function getTitle() {
return $this->title;
}
public function getDate() {
$this->date->setDate($this->date->format('Y'), $this->date->format('m'),$this->day);
if(in_array($this->date->format("D"),$this->exceptions)) {
$this->date->modify('next monday');
}
return $this->date;
}
public function setDate($modify) {
$this->date->modify($modify);
}
}
Testing.php<?php
namespace Event;
class Testing implements IEvent {
public $title = 'End of Month Testing Date';
private $day = 'last monday of this month';
private $exceptions = array('Sun','Sat','Fri');
private $date;
public function __construct() {
$this->date = new \DateTime($this->day);
}
public function getTitle() {
return $this->title;
}
public function getDate() {
if(in_array($this->date->format("D"),$this->exceptions)) {
$this->date->modify('previous thursday');
}
return $this->date;
}
public function setDate($modify) {
$this->date->modify($modify);
}
}