ahmedmii Posted August 7, 2011 Share Posted August 7, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/244106-datetime-field/ Share on other sites More sharing options...
requinix Posted August 7, 2011 Share Posted August 7, 2011 Want to try that explanation again? Because that one isn't very explanatory. Quote Link to comment https://forums.phpfreaks.com/topic/244106-datetime-field/#findComment-1253684 Share on other sites More sharing options...
ahmedmii Posted August 7, 2011 Author Share Posted August 7, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/244106-datetime-field/#findComment-1253769 Share on other sites More sharing options...
ahmedmii Posted August 8, 2011 Author Share Posted August 8, 2011 Come on guys, I really need your help Quote Link to comment https://forums.phpfreaks.com/topic/244106-datetime-field/#findComment-1254056 Share on other sites More sharing options...
phpSensei Posted August 8, 2011 Share Posted August 8, 2011 Hi Ahmed, Can you provide some code? I still don't understand what you are trying to accomplish... Take a deep breath and give a better explanation Quote Link to comment https://forums.phpfreaks.com/topic/244106-datetime-field/#findComment-1254059 Share on other sites More sharing options...
ahmedmii Posted August 8, 2011 Author Share Posted August 8, 2011 OK, shour=7 ehour=16 the field in database is datetime(2011-07-12 13:22:13) the function that I need is to do is after 24 working hours(three days) from the that time$date send email. CLEAR...... :'( :'( Quote Link to comment https://forums.phpfreaks.com/topic/244106-datetime-field/#findComment-1254100 Share on other sites More sharing options...
requinix Posted August 8, 2011 Share Posted August 8, 2011 Set up a cron job/scheduled task (Google it) that runs just a bit after hour, like at HH:05. It runs a script that looks for everything that happened between 24 and 25 hours ago and sends emails appropriately. Quote Link to comment https://forums.phpfreaks.com/topic/244106-datetime-field/#findComment-1254423 Share on other sites More sharing options...
jcbones Posted August 8, 2011 Share Posted August 8, 2011 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/244106-datetime-field/#findComment-1254607 Share on other sites More sharing options...
ahmedmii Posted August 9, 2011 Author Share Posted August 9, 2011 Thank you such a great gode. I've question is how can it skip the weekend days? Quote Link to comment https://forums.phpfreaks.com/topic/244106-datetime-field/#findComment-1254733 Share on other sites More sharing options...
ahmedmii Posted August 9, 2011 Author Share Posted August 9, 2011 Hi, about the code when I try to retrive the date from the database like this $database_date=mysql_query('select 'Assigned Date' from 'test'); It gave this message [/img] [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/244106-datetime-field/#findComment-1254747 Share on other sites More sharing options...
voip03 Posted August 9, 2011 Share Posted August 9, 2011 place the error in the insert code tag! Quote Link to comment https://forums.phpfreaks.com/topic/244106-datetime-field/#findComment-1254748 Share on other sites More sharing options...
ahmedmii Posted August 9, 2011 Author Share Posted August 9, 2011 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] Quote Link to comment https://forums.phpfreaks.com/topic/244106-datetime-field/#findComment-1254754 Share on other sites More sharing options...
the182guy Posted August 9, 2011 Share Posted August 9, 2011 You're passing the result of mysql_query() to your function which in turn passes it to explode() which is causing the error. You need to get the datetime string from the query result and pass that. Quote Link to comment https://forums.phpfreaks.com/topic/244106-datetime-field/#findComment-1254756 Share on other sites More sharing options...
ahmedmii Posted August 9, 2011 Author Share Posted August 9, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/244106-datetime-field/#findComment-1254759 Share on other sites More sharing options...
TeNDoLLA Posted August 9, 2011 Share Posted August 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244106-datetime-field/#findComment-1254868 Share on other sites More sharing options...
ahmedmii Posted August 9, 2011 Author Share Posted August 9, 2011 Did you mean like this [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/244106-datetime-field/#findComment-1254986 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.