kjtocool Posted October 25, 2008 Share Posted October 25, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/130070-solved-populating-a-table-with-dates/ Share on other sites More sharing options...
Barand Posted October 25, 2008 Share Posted October 25, 2008 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/130070-solved-populating-a-table-with-dates/#findComment-674519 Share on other sites More sharing options...
kjtocool Posted November 11, 2008 Author Share Posted November 11, 2008 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/130070-solved-populating-a-table-with-dates/#findComment-688010 Share on other sites More sharing options...
PFMaBiSmAd Posted November 11, 2008 Share Posted November 11, 2008 Php has a nasty built in 19 Jan 2038 03:14:07 problem with Unix Timestamps. Quote Link to comment https://forums.phpfreaks.com/topic/130070-solved-populating-a-table-with-dates/#findComment-688053 Share on other sites More sharing options...
Barand Posted November 11, 2008 Share Posted November 11, 2008 Shouldn't that be "Unix Timestamps have nasty built in 19 Jan 2038 03:14:07 problem" and not just PHP? Quote Link to comment https://forums.phpfreaks.com/topic/130070-solved-populating-a-table-with-dates/#findComment-688066 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.