Jump to content

[SOLVED] Populating a Table with Dates


kjtocool

Recommended Posts

Can anyone help point me in the right direction on how to go about populating a table in the way described below?

 

Basically I have a table: kj_weeks

week_id - int(10) ... AUTO_INCREMENT ... Primary Key

start_date - date ... NOT NULL

end_date - date ... NOT NULL

 

 

What I want to do, is populate this table starting now, and going out 100 years.  I want to simply populate it like so:

 

id - start_date - end_date

1 - 10/27/2008 - 11/02/2008

2 - 11/03/2008 - 11/09/2008

etc

 

Is there a painless way I can do this, other than manually entering weeks?

Link to comment
https://forums.phpfreaks.com/topic/130070-solved-populating-a-table-with-dates/
Share on other sites

I wouldn't populate any db table with dates in that totally useless format. Use columns of type DATE and use YYYY-MM-DD format.

 

try

<?php
$sd = '2008-10-27';
$st = strtotime($sd);
$et = strtotime('+100 years', $st);

while ($st < $et)
{
    $sdate = date ('Y-m-d', $st);
    $edate = date ('Y-m-d', strtotime('+6 days', $st));
    $sql = "INSERT INTO table (start_date, end_date) VALUES ('$sdate', '$edate')";
    mysql_query($sql);
    $st = strtotime ('+7 days', $st);
}

?>

  • 3 weeks later...

I modified your code only slightly, because it was causing an error.  I don't think it liked the +100 years bit, I tried 2099, but it didn't like that either, so I then tried 2031, which it took:

 

<?php
$sd = '2007-10-22';
$ed= '2031-10-22';
$st = strtotime($sd);
$et = strtotime($ed);
$databaseConnect = mysqli_connect("localhost", "username", "pass", "db");

while ($st < $et)
{
$sdate = date ('Y-m-d', $st);
$edate = date ('Y-m-d', strtotime('+6 days', $st));
$sql = "INSERT INTO table_name (start_date, end_date) VALUES ('$sdate', '$edate')";

mysqli_query($databaseConnect, $sql);
st = strtotime ('+7 days', $st);
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.