How to turn strotime output into a variable

Asked

Viewed 155 times

2

I’m trying to use this script, which returns every Monday between two periods:

$beginday = isset($_POST["Tinsem3"]) ? $_POST["Tinsem3"] : false;
$lastday  = isset($_POST["Tdesl"]) ? $_POST["Tdesl"] : false;

$lastday = strtotime($lastday);
for($i = strtotime('Monday', strtotime($beginday)); $i <= $lastday; $i = strtotime('+1 week', $i))
    echo date('l Y-m-d', $i);

I adapted to search the form data, and output between dates 08/04/2015 and 08/05/2015 is already correct:

Monday 2015-04-13

Monday 2015-04-20

Monday 2015-04-27

Monday 2015-05-04

What I want is to put each of these results within variables, so I can count how many seconds there are and use to do some calculations.

I’ve tried to do something like:

$tornararray = array (date('l Y-m-d', $i));

And then count the arrays, with something like:

$total2 = count(array_filter($tornararray)); 

But when do I give var_dumpof $tornararray, I get the exit:

array(1) { [0]=> string(17) "Monday 2015-05-04" }

From what I understand, he’s only picking up the last occurrence. How do I turn each of the results I get with echo date('l Y-m-d', $i); in variables?

Warning: This question is is not duplicate of this. These are different questions. Here I want to know how to include the results (from another script, not the one used in that question) within variables, while in that other one I have very different goals (adapt that working day script etc...).

2 answers

3

$arr = array();
for($i = strtotime('Monday', strtotime($beginday)); $i <= $lastday; $i = strtotime('+1 week', $i))
    array_push($arr, date('l Y-m-d', $i));

print_r($arr);

  • 2

    Only one addendum: array_push has the same effect as $array[] = $var - see the manual!

  • 2

    Yes. It’s true, @Papa Charlie. It’s even better to do this, because of preprocessing.

  • 2

    Yes, and in the end you’ll have to use count to count the items - it is inevitable

  • 1

    I tested it here and the two solutions worked! + 1 Dei as solved on @Papacharlie for having already included the count. Thanks to both!

3


I’m sorry if I misunderstood, but what you want is this?

$date = array();

for($i = strtotime('Monday', strtotime($beginday)); $i <= $lastday; $i = strtotime('+1 week', $i))
{
    $date[] = date('l Y-m-d', $i);
}

$total = count( $date );
  • 1

    Hello, take a look here array_push().

  • 1

    Array_push: Adiciona um ou mais elementos no final de um array, and what he wants is contar quantas segundas. Just create an array and count the indexes.

  • 2

    $array[] = $i is equivalent to array_push($array, $i). The first is even faster by not making function call.

  • 1

    What I meant to say is that your answer is pretty much the same as the other, you just used the other methodology, and you returned the value in the function count() :/ this can complicate the person who asked the question, and creates duplicate responses, I believe the most correct would be editar the above answer, and add these details.

Browser other questions tagged

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