Created
February 17, 2020 15:43
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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