Canman2005 Posted October 17, 2009 Share Posted October 17, 2009 Hi all I have the following table `date` date `day` int(2) `month` int(2) `year` int(4) If I pass a date range in PHP, such as $datefrom = '2009-02-20'; $dateto = '2009-02-25'; How can I get it to INSERT a record for every date between that date range? So with that example date range, it would do INSERT INTO `dates` (`date`, `day`, `month`, `year`) VALUES ('2009-02-20', '20', '02', '2009'), ('2009-02-21', '21', '02', '2009'), ('2009-02-22', '22', '02', '2009'), ('2009-02-23', '23', '02', '2009'), ('2009-02-24', '24', '02', '2009'), ('2009-02-25', '25', '02', '2009') but not do an INSERT if any of those dates already exists in the database. Any help would be great Thanks very much Ed Link to comment https://forums.phpfreaks.com/topic/178017-insert-for-date-range/ Share on other sites More sharing options...
Canman2005 Posted October 17, 2009 Author Share Posted October 17, 2009 Any ideas anyone? Thanks Link to comment https://forums.phpfreaks.com/topic/178017-insert-for-date-range/#findComment-938645 Share on other sites More sharing options...
Canman2005 Posted October 17, 2009 Author Share Posted October 17, 2009 Figured it out <?php function dates($start,$end) { $ret = array(); $sts = strtotime($start); $ets = strtotime($end); if ($ets < $sts) return (false); for($i=$sts;$i<=$ets;$i+=86400) $ret[] = $i; return($ret); } $dates = dates('2009-01-20','2009-02-05'); if (is_array($dates)) foreach ($dates as $dt) echo date('Y-m-d',$dt) . '<br>'; ?> Link to comment https://forums.phpfreaks.com/topic/178017-insert-for-date-range/#findComment-938673 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.