Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ConvertTimezones.php 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. class ConvertTimezones extends Command
  5. {
  6. /**
  7. * The name and signature of the console command.
  8. *
  9. * @var string
  10. */
  11. protected $signature = 'convert:timezones';
  12. /**
  13. * The console command description.
  14. *
  15. * @var string
  16. */
  17. protected $description = 'Converts countries csv to generate a timezones csv file';
  18. /**
  19. * Execute the console command.
  20. */
  21. public function handle(): int
  22. {
  23. $sourcePath = resource_path('data/countries.csv');
  24. $destinationPath = resource_path('data/timezones.csv');
  25. $source = fopen($sourcePath, 'rb');
  26. $destination = fopen($destinationPath, 'wb');
  27. fputcsv($destination, ['id', 'country_id', 'country_code', 'name', 'gmt_offset', 'gmt_offset_name', 'abbreviation', 'tz_name']);
  28. $idCounter = 1;
  29. $headers = fgetcsv($source);
  30. while (($row = fgetcsv($source)) !== false) {
  31. $rowAssoc = array_combine($headers, $row);
  32. $countryId = $rowAssoc['id'];
  33. $countryCode = $rowAssoc['iso_code_2'];
  34. $timezonesJson = $rowAssoc['timezones'];
  35. $timezonesArray = json_decode($timezonesJson, true);
  36. foreach ($timezonesArray as $timezone) {
  37. $newRow = [
  38. $idCounter++,
  39. $countryId,
  40. $countryCode,
  41. $timezone['zoneName'],
  42. $timezone['gmtOffset'],
  43. $timezone['gmtOffsetName'],
  44. $timezone['abbreviation'],
  45. $timezone['tzName'],
  46. ];
  47. fputcsv($destination, $newRow);
  48. }
  49. }
  50. fclose($source);
  51. fclose($destination);
  52. $this->info('Timezones csv file generated successfully.');
  53. return 0;
  54. }
  55. }