Jump to content

datetime field


ahmedmii

Recommended Posts

Hello,

 

I'm new in php and mysql too. The problem is the working hours is 9h starts from 7 to 16  . I need to do is decreament is the time 24h from the time in datetime field and for everydecreamnet increase the date. For example

2011-01-08 13:00:00

when I read it from database I want it like this frist time

end time which is 16:00:00 -13:00:00=3 date++. Now 24-3=21 and continue like this

 

how can I implement it?

Link to comment
https://forums.phpfreaks.com/topic/244106-datetime-field/
Share on other sites

Well, I've field datetime so, the work starts at 7 and ends at 16 so,working hours is 9hours .For example

value in the field is 2001-01-29 8:00:00 .The fucntion needs to send email after 24 working hours so, like this

since the work ends at 16 .It'll be like this 16:00:00-8:00:00 =8:00:00 ,then the first day finish so,increament date++ ,then we see how much hours left 24:00:00-8:00:00=16:00:00 ,then the second day 16:00:00-9:00:00(work day hours)=8:00:00 ,then ,increament date++, then we see how much hours left 16:00:00-8:00:00=8:00:00 ,then the thrid day 9:00:00-8:00:00=one hour the send email.

 

I HOPE IT'S CLEAR NOW.

 

THANK YOU

Link to comment
https://forums.phpfreaks.com/topic/244106-datetime-field/#findComment-1253769
Share on other sites

So you are starting work at 7, and ending work at 16 , and you want to send an email after 24 hours worked?

 

I couldn't think of a way to explain how to do it, and there may even be a better way, but this may help you.  Fully commented up.

 

<?php

$database_date = '2011-01-08 13:00:00'; //this comes from your database, 
$updated_time = strtotime('+' . hoursLeft($database_date) . ' hours',strtotime($database_date)); //Handle the date:
$new_date = date('m-d-Y, H:i:s',$updated_time); //this is when the email should be sent.

echo date('m-d-Y, H:i:s',strtotime($database_date)) . '<br />' . $new_date . '<-- you should be emailing at this time!'; //just echo's it to the screen.

function hoursLeft($time) { //this function handles the count of how many hours you need to fulfill your task.


////////Editable variables /////////////////////////////
$work_starts = 7; //start time;
$work_ends = 16;  //end time;
$time_to_email = 24;  //amount of time to allow before email goes out, (work hours).
///////End Editable Variables //////////////////////////


$parts = explode(' ',$time); //split the time off of the date.
list($hour, $minute, $second) = explode(':',$parts[1]); //get the hours, minutes, seconds.

if($minute == 0) { //if the minutes is over 0, then return a full hour for it
	$hours = $work_ends - $hour;
}
else { //otherwise, take into account that this isn't a full hour.
	$hours = ($work_ends - 1) - $hour;
}

for($i = $work_ends,$count = $hours; $count <= $time_to_email; $i++) { //Start the increment at 16, the count at the current hours, keep the count below 25, increment on each loop.
	if($i > 24) { //if the increment is over 24, reset it to 1.
		$i = 1;
	}

	if($i >= $work_starts && $i <= $work_ends) { //if the increment is between 7 and 16, add to the count, which will break the loop at 24.
		$count += 1;
	}
	$hours += 1; //add to the hours, the loop breaks at a count of 24, which will give us the total hours to add to the updated_time above.
}

return $hours; //return hours

}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/244106-datetime-field/#findComment-1254607
Share on other sites

This what I add to this code.
<?php$database_date = mysql_query('select 'AssignedDate' from 'test'')//this comes from your database, 
updated_time = strtotime('+' . hoursLeft($database_date) . ' hours',strtotime($database_date)); //Handle the date:$new_date = date('m-d-Y, H:i:s',$updated_time); //this is when the email should be sent. [b]line 15[/b]

$new_date = date('m-d-Y, H:i:s',$updated_time); //this is when the email should be sent.echo date('m-d-Y, H:i:s',strtotime($database_date)) . '<br />' . $new_date . '<-- you should be emailing at this time!'; //just echo's it to the screen. [b]line 16[/b]

function hoursLeft($time) { //this function handles the count of how many hours you need to fulfill your task.	
////////Editable variables /////////////////////////////
$work_starts = 7; //start time;	
$work_ends = 16;  //end time;	
$time_to_email = 24;  //amount of time to allow before email goes out, (work hours).	
///////End Editable Variables //////////////////////////

$parts = explode(' ',$time); //split the time off of the date.	[b]line25[/b]
list($hour, $minute, $second) = explode(':',$parts[1]); //get the hours, minutes, seconds.  [b]line26[/b]		

if($minute == 0) { //if the minutes is over 0, then return a full hour for it		
$hours = $work_ends - $hour;	}

else { //otherwise, take into account that this isn't a full hour.		
$hours = ($work_ends - 1) - $hour;	}

for
($i = $work_ends,$count = $hours; $count <= $time_to_email; $i++) { //Start the increment at 16, the count at the current hours, keep the count below 25, increment on each loop.

if($i > 24) { //if the increment is over 24, reset it to 1.			
$i = 1;		}

if($i >= $work_starts && $i <= $work_ends) { //if the increment is between 7 and 16, add to the count, which will break the loop at 24.			$count += 1;		}		
$hours += 1; //add to the hours, the loop breaks at a count of 24, which will give us the total hours to add to the updated_time above.	}		
return $hours; //return hours	}
?>

After run the code it gave me this message




 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/244106-datetime-field/#findComment-1254754
Share on other sites

I did like this ,but still it gave me the same messagge

$test=mysql_query('select 'AssignedDate' from 'test'');//this comes from your database, 

$database_date=$test;
and the rest is the same

 

$test is not the date, it is a MySQL resultset if there was records to be found. You need to use the mysql_fetch_* functions to fetch the actual data from the result set and then assign it to the variable. E.g. http://php.net/manual/en/function.mysql-fetch-row.php . You can see more choices on the left side of that page for assoc, object etc.

Link to comment
https://forums.phpfreaks.com/topic/244106-datetime-field/#findComment-1254868
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.