A
Anonymous
Guest
How can I store this value in my database? For example, this script will calculate the workdays only, when I enter 'Date From' and 'Date To' into the text box, it calculate the duration between 'Date From' and 'Date To', then store the 'Date From','Date To' and the 'Duration' into the database. But it only can store 'Date From' and 'Date To', it cannot store the 'Duration'. For example, the result is: "There are 6 work days from 2003/07/09 to 2003/07/16". How to store "6" into the 'Duration' column.
----------------------------------------------------------------------------------
form1.php (This form has two input boxes; FromDate and ToDate. When I click the submit button, it posts the two values into form2.php.
----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
form2.php (It should calculate the duration and store three values into the database.
----------------------------------------------------------------------------------
<? $Connect = mysql_connect("localhost","root","");
mysql_select_db("mw");
$result=mysql_query("insert into Date1 (FromDate, ToDate, Duration) values ('$FromDate', '$ToDate', '$Duration')");
mysql_close();
?>
<?php
function count_workdays($date1,$date2){
$firstdate = strtotime($date1);
$lastdate = strtotime($date2);
$firstday = date(w,$firstdate);
$lastday = date(w,$lastdate);
$totaldays = intval(($lastdate-$firstdate)/86400)+1;
//check for one week only
if ($totaldays<=7 && $firstday<=$lastday){
$workdays = $lastday-$firstday+1;
//check for weekend
if ($firstday==0){
$workdays = $workdays-1;
}
if ($lastday==6){
$workdays = $workdays-1;
}
}else { //more than one week
//workdays of first week
if ($firstday==0){
//so we don't count weekend
$firstweek = 5;
}else {
$firstweek = 6-$firstday;
}
$totalfw = 7-$firstday;
//workdays of last week
if ($lastday==6){
//so we don't count sat, sun=0 so it won't be counted anyway
$lastweek = 5;
}else {
$lastweek = $lastday;
}
$totallw = $lastday+1;
//check for any mid-weeks
if (($totalfw+$totallw)>=$totaldays){
$midweeks = 0;
} else { //count midweeks
$midweeks = (($totaldays-$totalfw-$totallw)/7)*5;
}
//total num of workdays
$workdays = $firstweek+$midweeks+$lastweek;
}
/*
check for and subtract and holidays etc. here
...
*/
return ($workdays);
} //end funtion count_workdays()
$date1 = "$FromDate";
$date2 = "$ToDate";
echo "There are ".count_workdays($date1,$date2)." work days from $date1 to $date2";
?>
----------------------------------------------------------------------------------
form1.php (This form has two input boxes; FromDate and ToDate. When I click the submit button, it posts the two values into form2.php.
----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
form2.php (It should calculate the duration and store three values into the database.
----------------------------------------------------------------------------------
<? $Connect = mysql_connect("localhost","root","");
mysql_select_db("mw");
$result=mysql_query("insert into Date1 (FromDate, ToDate, Duration) values ('$FromDate', '$ToDate', '$Duration')");
mysql_close();
?>
<?php
function count_workdays($date1,$date2){
$firstdate = strtotime($date1);
$lastdate = strtotime($date2);
$firstday = date(w,$firstdate);
$lastday = date(w,$lastdate);
$totaldays = intval(($lastdate-$firstdate)/86400)+1;
//check for one week only
if ($totaldays<=7 && $firstday<=$lastday){
$workdays = $lastday-$firstday+1;
//check for weekend
if ($firstday==0){
$workdays = $workdays-1;
}
if ($lastday==6){
$workdays = $workdays-1;
}
}else { //more than one week
//workdays of first week
if ($firstday==0){
//so we don't count weekend
$firstweek = 5;
}else {
$firstweek = 6-$firstday;
}
$totalfw = 7-$firstday;
//workdays of last week
if ($lastday==6){
//so we don't count sat, sun=0 so it won't be counted anyway
$lastweek = 5;
}else {
$lastweek = $lastday;
}
$totallw = $lastday+1;
//check for any mid-weeks
if (($totalfw+$totallw)>=$totaldays){
$midweeks = 0;
} else { //count midweeks
$midweeks = (($totaldays-$totalfw-$totallw)/7)*5;
}
//total num of workdays
$workdays = $firstweek+$midweeks+$lastweek;
}
/*
check for and subtract and holidays etc. here
...
*/
return ($workdays);
} //end funtion count_workdays()
$date1 = "$FromDate";
$date2 = "$ToDate";
echo "There are ".count_workdays($date1,$date2)." work days from $date1 to $date2";
?>