-1

I need help on this..
I am new in php and dummy..

$displaydate = "";

$paydate1 = "23-10-2016";
$paydate2 = "23-11-2016";   
$paydate3 = "23-12-2016";    
$paydate4 = "23-01-2017";    
$paydate5 = "23-02-2017";   
$paydate6 = "23-03-2017";




if todaydate is 23-10-2016 then $displaydate = "$paydate2" until 22-11-2016. When 23-11-2016 then $displaydate = "$paydate3" and so on.



then results    
if the date is 23-10-2016 until 22-11-2016 $displaydate = "$paydate2"   
if the date is 23-11-2016 until 22-12-2016 $displaydate = "$paydate3"    
if the date is 23-12-2016 until 22-01-2016 $displaydate = "$paydate4"   
if the date is 23-01-2016 until 22-02-2016 $displaydate = "$paydate5"  
if the date is 23-02-2016 until 22-03-2016 $displaydate = "$paydate6"   

please help with the code...
Thanks..

IM php DUMMY

Renne
  • 1
  • 1
  • 3
    This is a lot of code to ask someone to make for you with little input from you. I will say that in order to do this, i would cast the date from a string to a date (http://php.net/manual/en/datetime.createfromformat.php). PHP also allows you to compare two dates (http://stackoverflow.com/questions/8722806/how-to-compare-two-dates-in-php), so you would ensure that your date fits within the date range using a switch or series of if statements. This is something that you should do a little research and maybe learn some basic programming syntax before attempting – KM529 Oct 24 '16 at 16:45

1 Answers1

1

Organize your data convenient so you can easy access it. Put your paydates it an array:

$paydate = ["23-10-2016","23-11-2016","23-12-2016","23-01-2017","23-02-2017","23-03-2017"];

UPDATED per your comment:

$payout1 = $this->data->paymentdate1; 
$payout2 = $this->data->paymentdate2; 
$payout3 = $this->data->paymentdate3; 
$payout4 = $this->data->paymentdate4; 
$payout5 = $this->data->paymentdate5; 
$payout6 = $this->data->paymentdate6; 

$paydate = [$payout1,$payout2,$payout3,$payout4,$payout5,$pa‌​yout6];

Transform the dates in an easy to sort format:

function trf_date($date)
{
    return date('Y-m-d', strtotime($date));
}
$paydate = array_map("trf_date", $paydate );

Sort the array to be sure we will get the dates in ascending order while looping.

sort($paydate);

Now loop through the array to find the first date higher then today:

foreach($paydate as $key=>$val){
   if($val > date('Y-m-d'))
     break;
}

$displaydate = ''; //initialize the output variable

//check to see if the last retrieved date meets the condition not to be in the past. 
//this is for the case all dates in the array are in the past
if(isset($paydate[$key]) && $paydate[$key] > date('Y-m-d'))
   $displaydate = $paydate[$key];
moni_dragu
  • 1,163
  • 9
  • 16
  • i got error on this Notice: Undefined variable: displaydate – Renne Oct 27 '16 at 10:04
  • $payout1 = $this->data->paymentdate1; $payout2 = $this->data->paymentdate2; $payout3 = $this->data->paymentdate3; $payout4 = $this->data->paymentdate4; $payout5 = $this->data->paymentdate5; $payout6 = $this->data->paymentdate6; $paydate = ["$payout1","$payout2","$payout3","$payout4","$payout5","$payout6"]; function trf_date($date) { return date('Y-m-d', strtotime($date)); } $paydate = array_map("trf_date", $paydate ); sort($paydate); foreach($paydate as $key=>$val){ if($val > date('Y-m-d')) break; } if ($key > 0) $displaydate = $paydate[$key]; – Renne Oct 27 '16 at 10:12
  • Don't use the quotes in the $paydate array. Build it like this: `$paydate = [$payout1,$payout2,$payout3,$payout4,$payout5,$pa‌​yout6];` – moni_dragu Oct 27 '16 at 10:24
  • I just a dummy of php.. could u help me to complete this? Thanks u so much~~ – Renne Oct 27 '16 at 11:15
  • `if ($key > 0) ` is there to make sure you will not get _Undefined index_ error. Initialize your variable at the beginning of the script: `$displaydate = '';` – moni_dragu Oct 27 '16 at 11:21
  • so it is safe and can it follow date by date? i scare the date will become like go random.. currently i remove `if ($key > 0)` no more the Undefined variable error.. – Renne Oct 27 '16 at 11:24
  • I have updated the answer to also work for all dates in the array being in the past. – moni_dragu Oct 27 '16 at 11:43