Jozsef Naghi
Jozsef Naghi

Reputation: 1105

Php date issue algorithm

This is my code:

$current_day = strtotime(date("2015-04-20"));
for ($i=1; $i<= 5; $i ++){
   $choice_date = $get_choice_date->choice_date; // here i get dynamically the dates - y-m-d format
  if ( $current_day < strtotime($choice_date) ){ 
    if (strtotime(date("H:i:s")) < strtotime("17:00:00")){
         echo $i."-test";
    }
  }
}

Suppose that the current day is 2015-04-20. For instance: if it is 2015-04-20(monday) and the time is passed by 17:00:00 then don't display $i."-test"; - tuesday, but display the rest of the days; else if the time is before 17:00:00 then display tuesday and the rest of them. How can I do that?

Upvotes: 0

Views: 81

Answers (2)

enigma
enigma

Reputation: 3491

Think you want code similar to the following, if I understood your question correctly.

//$now = time();
$now = strtotime("2015-04-20 16:00:00");
$current_day = date("N", $now);
$current_hour = date("H", $now);
//$choice_date = strtotime($choice_date = $get_choice_date->choice_date);
$choice_date = strtotime("2015-04-28 18:00:00"); // an example date in future. Delete this line and uncomment the above.

$days = array("sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday");

if ($now < $choice_date) {
    $day_index = date("w", $now) + 1;
    if ($current_hour >= 17){
        $day_index++;
    }
    for ($i=$day_index; $i<= 5; $i ++){
        echo $days[$i] . $i."-test<br/>";
    }
}

The above gives the following output when the $now time is before 17:00,

tuesday2-test
wednesday3-test
thursday4-test
friday5-test

and the same output without Tuesday if it is after 17:00

If I have misunderstood, comment below to clarify.

Upvotes: 1

n-dru
n-dru

Reputation: 9430

You need to add another condition in case it is Monday after 17:00:

((strtotime(date("H:i:s")) >= strtotime("17:00:00") && $current_day == strtotime($choice_date) )

and change < to <= in comparison to $choice_date to let Monday in:

$current_day = strtotime(date("2015-04-20"));
for ($i=1; $i<= 5; $i ++){
   $choice_date = $get_choice_date->choice_date; // here i get dynamically the dates - y-m-d format
   if ($current_day <= strtotime($choice_date) ){ 
     if ((strtotime(date("H:i:s")) < strtotime("17:00:00")) || (strtotime(date("H:i:s")) >= strtotime("17:00:00") && $current_day == strtotime($choice_date))){
      echo $i."-test";

    }
  }
}

Upvotes: 0

Related Questions