Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

store values

General discussions related to php

Moderators: macek, egami, gesf

store values

Postby superman » Sat Jul 05, 2003 10:29 pm

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","");
$result=mysql_query("insert into Date1 (FromDate, ToDate, Duration) values ('$FromDate', '$ToDate', '$Duration')");

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";

New php-forum User
New php-forum User
Posts: 36
Joined: Fri Jun 27, 2003 11:54 pm

Postby frogrocker » Wed Jul 09, 2003 6:42 am

has the script defined the $Duration variable? unless it somehow passes it to the script as a variable, I cannot see where the variable has been defined.

If it hasnt been defined, it wont insert it into the database...

(But don't blame me if it's wrong... I'm only learning!)
User avatar
New php-forum User
New php-forum User
Posts: 98
Joined: Mon Jul 07, 2003 10:30 pm
Location: Manchester

Return to PHP General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.