jeudi 20 septembre 2018

Set just month and day with PHP

The following if-statment checks what option is selected in my dropdown menu.

If H17, H18, H19, H20 and H21 is selected, the code adds 1 year on the date time. But now the code should set the month to 12 and the day to 31 with the options H17, H18, H19, H20 and H21 in the dropdown menu.

<?php
if($_POST['EventCode']=='H17' )
{
     $datum_after_three_years = DateTime::createFromFormat('Y-m-d', $datum);
      $datum_after_three_years ->modify('+1 years');
  echo $datum_after_three_years ->format('Y-m-d');

}elseif($_POST['EventCode']=='H18' )
{
     $datum_after_three_years = DateTime::createFromFormat('Y-m-d', $datum);
      $datum_after_three_years ->modify('+1 years');
  echo $datum_after_three_years ->format('Y-m-d');

}elseif($_POST['EventCode']=='H19' )
{
     $datum_after_three_years = DateTime::createFromFormat('Y-m-d', $datum);
      $datum_after_three_years ->modify('+1 years');
  echo $datum_after_three_years ->format('Y-m-d');
}elseif($_POST['EventCode']=='H20' )
{
     $datum_after_three_years = DateTime::createFromFormat('Y-m-d', $datum);
      $datum_after_three_years ->modify('+1 years');
  echo $datum_after_three_years ->format('Y-m-d');
}elseif($_POST['EventCode']=='H21' )
{
     $datum_after_three_years = DateTime::createFromFormat('Y-m-d', $datum);
      $datum_after_three_years ->modify('+1 years');
  echo $datum_after_three_years ->format('Y-m-d');


}else
{
  $datum_after_three_years = DateTime::createFromFormat('Y-m-d', $datum);
   $datum_after_three_years ->modify('+3 years');
echo $datum_after_three_years ->format('Y-m-d');


}




 ?>

I tried:

      $datum_after_three_years = DateTime::createFromFormat('Y-m-d', $datum);
      $datum_after_three_years ->modify('+1 years');
      $datum_after_three_years ->modify('Y-12-31');
 echo $datum_after_three_years ->format('Y-m-d');

I'm a PHP starter so my attempt is probably not really logically correct.

regards!:)

Aucun commentaire:

Enregistrer un commentaire