Skip to content

Instantly share code, notes, and snippets.

@paulovitorbal
Created February 17, 2020 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulovitorbal/9db156dd36fd99badc121da5c2fe1a32 to your computer and use it in GitHub Desktop.
Save paulovitorbal/9db156dd36fd99badc121da5c2fe1a32 to your computer and use it in GitHub Desktop.
Creates a CSV file to be imported on google calendar with the pregnancy weeks from a base date
<?php
class Evento {
public $subject;
public $startDate;
public $startTime = '';
public $endDate = '';
public $endTime = '';
public $allDay = 'True';
public $description = '';
public $location = '';
public $private = '';
public function toArray():array {
return get_object_vars($this);
}
public static function getHeaders():array {
return [
'Subject',
'Start Date',
'Start Time',
'End Date',
'End Time',
'All day event',
'Description',
'Location'
];
}
}
$fh = fopen('php://memory', 'wb+');
fputcsv($fh, Evento::getHeaders());
$base = DateTimeImmutable::createFromFormat('Y/m/d', '2019/10/25');
foreach(range(0, 42) as $week) {
$evento = new Evento;
$evento->subject = sprintf('%d semana de gestação', $week);
$evento->startDate = $base->add(DateInterval::createFromDateString($week . ' weeks'))->format('Y/m/d');
$evento->endDate = $base->add(DateInterval::createFromDateString($week . ' weeks'))->format('Y/m/d');
fputcsv($fh, $evento->toArray());
}
fseek($fh, 0);
$result= stream_get_contents($fh);
fseek($fh, 0);
file_put_contents('~/Downloads/agenda_gestacional.csv', $result);
echo $result . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment