1

Possible Duplicate:
Print Y-m-d list of business dates between two dates from MySQL using PHP

I'm new to these kind of PHP programming so bear with me.

I want users to be able to post two dates, a startdate and a enddate and from these dates calculate the business dates (exclude Saturday and Sunday) and insert these to MySQL

For example if I have this form...

<form action='update.php' method='post'>
  <input type='text' name='start_date'>
  <input type='text' name='end_date'>
  <input type='submit' name='submit'>
</form>

update.php

if(isset($_POST['submit'])) {

   $start_date = $_POST['start_date']; //Let's say this is 2012-10-01
   $send_date = $_POST['end_date']; //Let's say this is 2012-10-10

}

Now I would like to loop through the dates and create a MySQL query that would run like this:

INSERT INTO tbl_dates (date_value) VALUES ('2012-10-01');
INSERT INTO tbl_dates (date_value) VALUES ('2012-10-02');
INSERT INTO tbl_dates (date_value) VALUES ('2012-10-03');
INSERT INTO tbl_dates (date_value) VALUES ('2012-10-04');
INSERT INTO tbl_dates (date_value) VALUES ('2012-10-05');
INSERT INTO tbl_dates (date_value) VALUES ('2012-10-08');
INSERT INTO tbl_dates (date_value) VALUES ('2012-10-09');
INSERT INTO tbl_dates (date_value) VALUES ('2012-10-10');

I know this is probably not a good solution but it's how I need it.

How do I create the loop?

Community
  • 1
  • 1
David
  • 1,171
  • 8
  • 26
  • 48

3 Answers3

1
$startDate = $start_date;
$endDate = $send_date;

// Convert to UNIX timestamps
$currentTime = strtotime($startDate);
$endTime = strtotime($endDate);

// Loop until we reach the last day
$result = array();
while ($currentTime <= $endTime) {
  if (date('N', $currentTime) < 6) {
    $result[] = date('Y-m-d', $currentTime);
  }
  $currentTime = strtotime('+1 day', $currentTime);
}

// start insertion
foreach($result as $value)
{
  mysql_query("INSERT INTO tbl_dates (date_value) VALUES '".$value."'");
}

Source : Print Y-m-d list of business dates between two dates from MySQL using PHP

Omg! I just realized that it's a thread started by you. You already have your answer

Community
  • 1
  • 1
asprin
  • 9,579
  • 12
  • 66
  • 119
  • 1
    do not copy code from other questions on StackOverflow please. If an answer exists you can copy code from, it means the question you want to paste it to is a duplicate. – Gordon Oct 31 '12 at 09:10
  • The section I copied was a part of the solution. I also included the next part to complete the solution. And that is the reason why I also mentioned the source in my post. – asprin Oct 31 '12 at 11:41
0

Use date('N', $timestamp) to get the number of the day of the week. You can then check if this day matches Your requirements.

P.S. date('N') was introduced in PHP 5.1.0

undefined
  • 2,051
  • 3
  • 26
  • 47
0

Code in your flavor on the basis of your PHP tag only ->

<?php

$_POST['start_date'] = '2012-10-01';
$_POST['end_date'] = '2012-10-10';

$start = explode('-',$_POST['start_date']);
$end   = explode('-',$_POST['end_date']);
$i = (int)$start[2];
for($i; $i<$end[2]; $i++)
{
$i = sprintf('%02d', $i);
echo "INSERT INTO tbl_dates (date_value) VALUES ('2012-10-$i')";
echo "<br />";
}

?>

Output

INSERT INTO tbl_dates (date_value) VALUES ('2012-10-01')
INSERT INTO tbl_dates (date_value) VALUES ('2012-10-02')
INSERT INTO tbl_dates (date_value) VALUES ('2012-10-03')
INSERT INTO tbl_dates (date_value) VALUES ('2012-10-04')
INSERT INTO tbl_dates (date_value) VALUES ('2012-10-05')
INSERT INTO tbl_dates (date_value) VALUES ('2012-10-06')
INSERT INTO tbl_dates (date_value) VALUES ('2012-10-07')
INSERT INTO tbl_dates (date_value) VALUES ('2012-10-08')
INSERT INTO tbl_dates (date_value) VALUES ('2012-10-09')
swapnesh
  • 26,318
  • 22
  • 94
  • 126