PHP :: Aufgabe #262

1 Lösung Lösung öffentlich

Welcher Tag ist morgen?

Anfänger - PHP von DragStar - 06.04.2020 um 08:30 Uhr
Verfassen Sie ein Programm, welches nach Eingabe von Tag, Monat und Jahr das Datum des nächsten Tages ausgibt.
Es sollen Monats- und Jahreswechsel, sowie Schaltjahre berücksichtigt werden.

Lösungen:

vote_ok
von Exception (7090 Punkte) - 12.04.2020 um 11:16 Uhr
main.php
Quellcode ausblenden PHP-Code
<?php

if (sizeof($argv) !== 4) // script name + 3 params 
{
  die('invalid argument number (' . (sizeof($argv) - 1) . '), three needed.' . PHP_EOL);
}

try
{
  $today = new DateTime("{$argv[1]}-{$argv[2]}-{$argv[3]}");

  echo 'TODAY:    ' , $today->format('Y-m-d') , PHP_EOL;
  echo 'TOMORROW: ' , $today->add(new DateInterval('P1D'))->format('Y-m-d') , PHP_EOL;
}
catch (Exception $ex)
{
  echo $ex->getMessage() , PHP_EOL;
}

Konsolenausgabe:

php main.php 2020 4 12
TODAY: 2020-04-12
TOMORROW: 2020-04-13

Konsolenausgabe:

php main.php 2020 4 30
TODAY: 2020-04-30
TOMORROW: 2020-05-01

Konsolenausgabe:

php main.php 2020 4 32
DateTime::__construct(): Failed to parse time string (2020-4-32) at position 8 (2): Unexpected character