0

I know that to show dates between two dates in Php we can use this code

$tsDateFrom = date('2015-05-01');
$tsDateTo = date('2015-05-07');
for($i=$tsDateFrom;$i<=$tsDateTo;$i++) {
   echo $thisDate = $i."<br>" ;
}

displays

2015-05-01
2015-05-02
2015-05-03
2015-05-04
2015-05-05
2015-05-06
2015-05-07

but how about this?

$tsDateFrom1 = date('2015-05-01');
$tsDateTo1 = date('2015-05-07');
for($i=$tsDateFrom1;$i<=$tsDateTo1;$i = strtotime('+1 day', $i)) {
  echo $thisDate = $i."<br>";
}

displays

2015-05-01

I need explanation since I'm only new to php and I want to learn how loops works.And why the second loop return just the first date?

  • well it looks like you have error reporting turned off, as when I run your second loop code it says `E_NOTICE : type 8 -- A non well formed numeric value encountered`. hint: it is not loop related, but dealing with `$i = strtotime('+1 day', $i)`. hint hint: `strtotime()` does not return a date like `2015-05-01` – Sean May 28 '15 at 02:49
  • @Sean i want to know why the first one returns all dates between two dates while the second one just return the first date –  May 28 '15 at 02:51
  • Because your `$i = strtotime('+1 day', $i)` causes your loop to fail. You get the error message `E_NOTICE : type 8 -- A non well formed numeric value encountered` as on the 1st loop `$i = 2015-05-01`, but on the second loop `$i = 88415`, so it is no longer in the format of `Y-m-d` – Sean May 28 '15 at 02:56
  • You need to covert `$i` to a timestamp first using `strtotime()` and then convert it back to your `Y-m-d` format. A little messy, but you could do `$i = date('Y-m-d, strtotime('+1 day', strtotime($i)))` – Sean May 28 '15 at 03:05

3 Answers3

1

First, you're not using date() properly:

$d = date('2015-05-01');

That will format the current date/time according to your pattern, e.g. "Y-m-d", but because those format specifiers are missing it will just return what you have passed it, i.e. a string.

Second, strtotime() expects the second argument to be a timestamp, not a string; when a non-numeric string is supplied, it will complain and return something you probably didn't expect:

$ var_dump(strtotime('+1 day', '2015-05-07'));
PHP Notice:  A non well formed numeric value encountered in php shell code on line 1
int(88415)

As you can see, it doesn't return a string either, but an integer value that represents a timestamp.

You could write it like so:

$d = date('Y-m-d', strtotime("$d +1 day"));

But, in this case it would be a better idea to use DatePeriod instead:

$tsDateFrom = new DateTime('2015-05-01 00:00:00');
$tsDateTo = new DateTime('2015-05-07 23:59:59');

foreach (new DatePeriod($tsDateFrom, new DateInterval('P1D'), $tsDateTo) as $date) {
        echo $date->format('Y-m-d'), '<br>';
}
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

$i = strtotime('+1 day', $i) returns an integer and it is not comparable in $i<=$tsDateTo1.

Maybe $i=date('m-d-Y',strtotime($i. "+1 days")); would work.

You can take a look at PHP - add 1 day to date format mm-dd-yyyy.... it might be useful

Community
  • 1
  • 1
Good Luck
  • 1,104
  • 5
  • 12
  • OP has `Y-m-d` so `...date('m-d-Y',...` probably is not the right code example to use. – Sean May 28 '15 at 02:59
0

From the PHP doc:

int strtotime ( string $time [, int $now = time() ] )

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

So, for example:

echo(strtotime("+1 day"));

Output:

1432868203

Then, as you see strtotime() doesnt returns something in the format you are expecting, since you are working with yy-mm-dd.

In fact, strtotime('+1 day', $i) is throwing an error in your code, and because that your loop fails.

Two advices:

  1. Read the doc
  2. Turn on PHP error reporting, this will give you some indication of where your errors are.
JosEduSol
  • 5,268
  • 3
  • 23
  • 31
  • so whats strtotime("+1 day") mean?.Does it mean add one day to s current datetime? –  May 28 '15 at 03:15
  • Yes. But your biggest problem is in your synyax. Look the doc, and check what arguments are you passing to `strtotime()`. – JosEduSol May 28 '15 at 03:25