Jump to content

php all dates between 2 dates?


mikefrederick

Recommended Posts

Thanks ahead of time:

 

If I have two dates , say February 28, 2008 and March 28, 2008, how can I echo every date between them? I supposed I could calculate the difference between the two and do for($day=0, $day<=$difference, $day++) and then do a day+$i type of thing, any other suggestions?

Link to comment
Share on other sites

<?php
$date1 = strtotime("February 28, 2008");
$date2 = strtotime("March 28, 2008");
for($i=date1; $i<=$date2; $i+=86400){
echo date("Y-m-d", $i);
}
?>

 

Ray

 

I'm trying to do same thing and having some problems...

if I run your script I'm getting days counted since 1970...

[tomas@dummy array]$ php test.php | wc -l

13967

 

I tried same using

<?php
$start_day = "01";
$start_month = "01";
$start_year = "2007";
$end_day = "10";
$end_month = "01";
$end_year = "2007";

$start_date = date('Ymd', mktime(0, 0, 0, $start_month, $start_day, $start_year));
$end_date = date('Ymd', mktime(0, 0, 0, $end_month, $end_day, $end_year));

for ($i = $start_date; $i < $end_date; $i++)
{
echo "$i \n";
$newdate = date('c', $i);
echo "$newdate \n";
}
?>

http://www.phpfreaks.com/forums/index.php/topic,185565.msg830818.html#msg830818

Link to comment
Share on other sites

I run the script above I get

 

2008-02-28

2008-02-29

2008-03-01

2008-03-02

2008-03-03

2008-03-04

2008-03-05

2008-03-06

2008-03-07

2008-03-08

2008-03-09

2008-03-10

2008-03-11

2008-03-12

2008-03-13

2008-03-14

2008-03-15

2008-03-16

2008-03-17

2008-03-18

2008-03-19

2008-03-20

2008-03-21

2008-03-22

2008-03-23

2008-03-24

2008-03-25

2008-03-26

2008-03-27

 

 

Ray

Link to comment
Share on other sites

How about this:

<?php
$seed = "February 28, 2008";
$start = strtotime($seed);
$end = strtotime("March 28, 2008");

$i = 0;
while ($start <= $end)
{
  $start = strtotime("{$seed} +{$i} days");
  echo date('F j, Y', $start) . "<br />\n";
  $i++;
}
?>

Link to comment
Share on other sites

This worked for me!

<?php
$start_day = "01";
$start_month = "02";
$start_year = "2008";
$end_day = "01";
$end_month = "03";
$end_year = "2008";

$start_date = mktime(0, 0, 0, $start_month, $start_day, $start_year);
$end_date = mktime(0, 0, 0, $end_month, $end_day, $end_year);

for ($i = $start_date; $i < $end_date; $i+=86400)
{
$newdate = date('Y-m-d', $i);
echo "$newdate \n";
}
?>

 

[tomas@dummy array]$ php test1.php

2008-02-01

2008-02-02

2008-02-03

2008-02-04

2008-02-05

2008-02-06

2008-02-07

2008-02-08

2008-02-09

2008-02-10

2008-02-11

2008-02-12

2008-02-13

2008-02-14

2008-02-15

2008-02-16

2008-02-17

2008-02-18

2008-02-19

2008-02-20

2008-02-21

2008-02-22

2008-02-23

2008-02-24

2008-02-25

2008-02-26

2008-02-27

2008-02-28

2008-02-29

 

leap year works too :)

 

Link to comment
Share on other sites

How about this:

<?php
$seed = "February 28, 2008";
$start = strtotime($seed);
$end = strtotime("March 28, 2008");

$i = 0;
while ($start <= $end)
{
  $start = strtotime("{$seed} +{$i} days");
  echo date('F j, Y', $start) . "<br />\n";
  $i++;
}
?>

I tried following

<?php
$seed = "2008-02-01";
$start = strtotime($seed);
$end = strtotime("2008-03-01");

$i = 0;
while ($start < $end)
{
  $start = strtotime("{$seed} +{$i} days");
  echo date('Y-m-d', $start) . "\n";
  $i++;
}
?>

for some reason first day of march is also included...

[tomas@dummy array]$ php test3.php

2008-02-01

2008-02-02

2008-02-03

2008-02-04

2008-02-05

2008-02-06

2008-02-07

2008-02-08

2008-02-09

2008-02-10

2008-02-11

2008-02-12

2008-02-13

2008-02-14

2008-02-15

2008-02-16

2008-02-17

2008-02-18

2008-02-19

2008-02-20

2008-02-21

2008-02-22

2008-02-23

2008-02-24

2008-02-25

2008-02-26

2008-02-27

2008-02-28

2008-02-29

2008-03-01

 

this works well if I'm putting $end = strtotime("2008-03-00"), but that's not really a date...

Link to comment
Share on other sites

for some reason first day of march is also included...

 

Ugh... my stupidity on that one. Try this as a fix:

<?php
$seed = "February 28, 2008";
$start = strtotime($seed);
$end = strtotime("March 28, 2008");

$i = 0;
while ($start < $end)
{
  echo date('F j, Y', $start) . "<br />\n";
  $i++;
  $start = strtotime("{$seed} +{$i} days");
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.