get PHP data, with conditions

Asked

Viewed 265 times

-2

I would like to take the date the user entered as the basis for the information he will select. There will be 4 date options, but the conditions are: if the user selects a day that has already passed, he will send me the day with the following month. Exp.: Today 18/03/2019 he can choose between: day 5; day 10; day 15; day 20; if he chooses day 5, 5<18, sends me the date 05/04/2019; if he chooses day 20, 20>18, sends me the date 20/03/2019;

So far I’ve made this PHP, however it is returning 0 on the dates, I would like your help to find what may be going wrong.

<?php
$data = date('d/m/Y');
$dataS = explode("/", $data);
$dia = $dataS[0];
$mes = $dataS[1];
$ano = $dataS[2];
?>
<?php
if($dia > 5){
    $mes = date('m',strtotime('+1month'));
    $d5 = $mes;
}
elseif($dia <= 5){
    $mes = $dataS[1];
    $d5 = $mes;
}
?>
<?php
if($dia > 10){
    $mes = date('m',strtotime('+1month'));
}
elseif($dia <= 10){
    $mes = $dataS[1];
    $d10 = $mes;
}
?>
<?php
if($dia > 15){
    $mes = date('m',strtotime('+1month'));
}
elseif($dia <= 15){
    $mes = $dataS[1];
    $d15 = $mes;
}
?>
<?php
if($dia > 20){
    $mes = date('m',strtotime('+1month'));
}
elseif($dia <= 20){
    $mes = $dataS[1];
    $d20 = $mes;
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento sem título</title>
</head>
Data hoje:<?php echo $data ?>;<br>Dia:<?php echo $data[0] ?>;<br>Mês:<?php echo $data[1] ?>;<br>Ano:<?php echo $data[2] ?>;<br>Dia 5:<?php echo $d5 ?>;<br>Dia 10:<?php echo $d10 ?>;<br>Dia 15<?php echo $d15 ?>;<br>Dia 20<?php echo $d20 ?>
<body>
</body>
</html>
  • $dia = $data[0], shouldn’t be $dataS[0] in place of $data[0]?

  • yes, I saw it, I corrected it, but the error still stands

  • I’ll update the post, for how this now.

  • now when I test it, it returns the date, but the 'explode' is not going correctly, it returns "1"; "8", "/". Instead of "18", "03", "2019".

  • The explode works yes: https://ideone.com/gPxyGx

  • as you can see, I did msm way in what I showed in the question...

Show 1 more comment

1 answer

-1

the problem was that 'explode()' does not work properly in 'date()'. then I traded for 3 'date()'.

<?php
$dataD = date('d');
$dataM = date('m');
$dataY = date('Y');
?>
<?php
if($dataD > 5){
	$mes = date('m',strtotime('+1month'));
	$d5 = $mes;
}
elseif($dataD <= 5){
	$mes = $dataM;
	$d5 = $mes;
}
?>
<?php
if($dataD > 10){
	$mes = date('m',strtotime('+1month'));
	$d10 = $mes;
}
elseif($dataD <= 10){
	$mes = $dataM;
	$d10 = $mes;
}
?>
<?php
if($dataD > 15){
	$mes = date('m',strtotime('+1month'));
	$d15 = $mes;
}
elseif($dataD <= 15){
	$mes = $dataM;
	$d15 = $mes;
}
?>
<?php
if($dataD > 20){
	$mes = date('m',strtotime('+1month'));
}
elseif($dataD <= 20){
	$mes = $dataM;
	$d20 = $mes;
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento sem título</title>
</head>
Data hoje:<?php echo $data ?>;<br>Dia 5:<?php echo $d5 ?>;<br>Dia 10:<?php echo $d10 ?>;<br>Dia 15:<?php echo $d15 ?>;<br>Dia 20:<?php echo $d20 ?>
<body>
</body>
</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.