Recognize dates as hours in PHP

Hi,

I have a script that posts my form data in a database. In this form are two dates, a startdate and an enddate. The script works perfect. But there is also a ‘hours’ field that should be filled by the amount of days times 8. If the startdate is 27-11-2014, and the enddate is 28-11-2014, the script should recognize that this are 2 days and writes 16 hours in the ‘hours’ column.

Controller

$scope.addNewRequest = function(){
        
        var data = {
        "employeeid": LoggedOnProfile[0].employeeid,
        "startdate": $scope.requestData.startdate, 
        "enddate": $scope.requestData.enddate, 
        "type": $scope.requestData.type, 
        "reason": $scope.requestData.reason, 
        "notes": $scope.requestData.notes
        };
        
        console.log("inserted Successfully", data);
    $http.post("http://url/to/insertscript.php", data,{"employeeid": LoggedOnProfile[0].employeeid, "startdate": $scope.requestData.startdate, "enddate": $scope.requestData.enddate, "type": $scope.requestData.type, "reason": $scope.requestData.reason, "notes": $scope.requestData.notes})
        .success(function(data, status, headers, config){
            console.log("inserted Successfully", data);
        });
        
        $scope.loadingIndicator = $ionicLoading.show({
		    content: 'Loading Data',
		    animation: 'fade-in',
		    showBackdrop: false,
		    maxWidth: 200,
		    showDelay: 100
	    });
        
        $timeout(function() {
	        $scope.closeForm();
	        $scope.loadingIndicator.hide();
	        $scope.requestData.startdate = ""; 
	        $scope.requestData.enddate = ""; 
	        $scope.requestData.type = "";
	        $scope.requestData.reason = "";
	        $scope.requestData.notes = "";
	    }, 1000); 
    };   

PHP script:

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $data = file_get_contents('php://input');
    $vars = json_decode($data);
    $startdate = $vars->startdate;
    $enddate = $vars->enddate;
    $type = $vars->type;
    $reason = $vars->reason;
    $notes = $vars->notes;
    $date = date('Y-m-d H:i');
    $employeeid = $vars->employeeid; 
    
    
    $sql = "INSERT INTO Requests (employeeid, date, startdate, enddate, type, reason, notes, status, hours, comment)
    VALUES ('$employeeid', '$date', '$startdate', '$enddate', '$type', '$reason', '$notes', 'Pending', '', '')";
    
    // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully";
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;

Has someone an idea how to do this?

Thanks in advance,

Jan

1 Like

Edit:
Had already a little script but it needs some improvement:

$startTimeStamp = strtotime($startdate);
$endTimeStamp = strtotime($enddate);
$timeDiff = abs($endTimeStamp - $startTimeStamp);
$numberDays = $timeDiff/86400;  // 86400 seconds in one day

// and you might want to convert to integer
$numberDays = intval($numberDays);
var_dump($numberDays); //returns int(1)

This script works… Almost…
the startdate is 27/11/2014 and the enddate is 28/11/2014 this returns 1 day. But should be 2.
the startdate is 27/11/2014 and the enddate is 27/11/2014 this returns 0 day. But should be 1.

Solved it

$dailyHours = '8';
$extraDay = '1';

$startTimeStamp = strtotime($startdate);
$endTimeStamp = strtotime($enddate);
$timeDiff = abs($endTimeStamp - $startTimeStamp);
$numberDays = $timeDiff/86400;  // 86400 seconds in one day

// and you might want to convert to integer
$numberDays = intval($numberDays);
$days = ($numberDays + $extraDay)
$hours = ($days * $dailyHours);
var_dump($hours);

Sorry for posting this too fast but maybe the next developer can use this now :wink:

1 Like

Exclude the Saturdays and Sundays?

$start = new DateTime($startdate);
	$end = new DateTime($enddate);
	// otherwise the  end date is excluded (bug?)
	$end->modify('+1 day');
	
	$interval = $end->diff($start);
	
	// total days
	$days = $interval->days;
	
	// create an iterateable period of date (P1D equates to 1 day)
	$period = new DatePeriod($start, new DateInterval('P1D'), $end);
	
	// best stored as array, so you can add more than one
	$holidays = array('2012-09-07');
	
	foreach($period as $dt) {
	    $curr = $dt->format('D');
	
	    // for the updated question
	    if (in_array($dt->format('Y-m-d'), $holidays)) {
	       $days--;
	    }
	
	    // substract if Saturday or Sunday
	    if ($curr == 'Sat' || $curr == 'Sun') {
	        $days--;
	    }
	}

	$hours = ($days * $dailyHours);