PHP - Date / Time Difference
Hey guys,
What i would like to create is the ability to calculate the difference between two dates / times. For example: 2010-12-13 10:00:00 to 2010-12-17 17:00:00 In a format of days / hours and minutes / seconds. However, I only want to calculate for an 9-5 work day, not the hours outside. Is this possible? If so, how would I go about creating something like this. Similar TutorialsHey guys, How would I go about subtracting Today from a previous day to find the difference? For example, I want to subtract TODAY from a previous date in my database, to determine if the difference is greater than 1 day. Any ideas? I tried doing the subraction in TIMESTAMPS, but when I convert the date back to Y-m-d H:i:s, I got some weird year and time. hiii, Im wanting to calculate the time difference between $out2 and $in2 and out put it into a spreadsheet: Code: [Select] $m=0; while ($m < $num9) { $fullname=mysql_result($result9,$m,"username"); $date=mysql_result($result9,$m,"date"); $in1=mysql_result($result9,$m,"in1"); $out1=mysql_result($result9,$m,"out1"); $in2=mysql_result($result9,$m,"in2"); $out2=mysql_result($result9,$m,"out2"); $out2f=date("G:i:s",strtotime($out2)); $in2f=date("G:i:s",strtotime($in2)); echo date("D",strtotime($date)). "\t" . $fullname . "\t" . $in1 . "\t". $out1 . "\t" . $in2 . "\t" . $out2 . "\t" . date("G:i:s",strtotime($out2f-$in2f)) . "\t" . "\n"; $m++; } header("Content-disposition: attachment; filename=spreadsheet.xls"); why does this code return 00:00:00 ? i have searched everywhere and no code seems to work. i have time1 and time 2 in the format Y-m-d H:i:s. i just don't know how to find the difference. old time: 2011-04-11 20:15:46 new time: 2011-04-11 21:00:11 after i have the difference, i need to get a resource production per hour which is stored in the database and calculate the new value of the resource. $production_per_hour = 100; $time_old = "2011-04-11 20:15:46"; $time_new = "2011-04-11 21:00:11"; $current_resources = "500"; Hi, I am trying to get the difference in time in mins, hours etc thingy. Now, for some reason I cannot get the right minutes or hours difference between two time stamps. Here is my code: Code: [Select] $postedTime=strtotime($comment_from_db->date_posted); $currentTime=time(); $mins=floor(($currentTime-$postedTime)/60); if($mins<=1){ echo $mins." minute ago by"; }elseif($mins>1){ if($mins>60){ $hrs=floor($mins/60); if($hrs>24){ }else{ if($hrs<2){ echo $hrs. " hour ago by"; }else{ echo $hrs. " hours ago by"; } } }else{ echo $mins." minutes ago by"; } } Now, for a comment posted like 25 mins ago it shows it was posted 4 hours ago. For a comment posted around 10 hours ago it shows it was posted 16 hours ago. What is the problem here? I was doing a test and checked the time in seconds after an interval of 2 mins. Here are the results: Comment posted: 1306669490 Current time: 1306683920 The difference is around 14430. This is not equal to 2 minutes. So, I am starting to think if the time I am getting is wrong? Should it have 10 digits? or less? I am not sure what is going on. Please, advice. Thanks. Problem; Need to check to see if someone who requested a password re-set has done so with in 24hrs. IF not- then it needs to prompt them to re-submit the request and deletes the previous re-submit code sent to them in their email. I have everything else figured out (deleting re-set code) but I have not figured the checking of if they have done so within the 24hr time period. Please tell me if the code below will accurately tell me if they complied with the 24 hour rule. $time1 = '1284675867';// logged at the time of password re-set request. Retrieved from database $time2 = time();// captured in script when they actually re-set their password $difference_between = $time2 - $time1; if($difference_between >= '5,200,000') { echo "Sorry- you have exceeded the 24 hr rule and need to re-submit your request"; } Compared to all that I have seen.........it appears rather simplistic. Am I missing something or will this simple approach work? For some reason when I echo $diff and $time_remaining it ALWAYS echos with values of 3 and 7 and not sure why. Code: [Select] $lock_date = $row['lock_date']; $current_time = time(); // Find out if user is locked out of their account if (($lock_date != "0000-00-00 00:00:00") && strtotime($lock_date) < $current_time) { $lock_date = strtotime($lock_date); $diff = $current_time - $lock_date; $diff = floor($diff/6000); // Take minutes and perform tasks if ($diff <= 10) { // Calculate time remaining $time_remaining = 10 - $diff; // Account locked error $errors = true; $message = "Account is locked! Must wait " .$time_remaining." minutes to log in again!"; $output = array('errorsExist' => $errors, 'message' => $message); } else { // Clear the lock $query = "UPDATE users_logins_attempts SET lockDate = NULL, ip_address = NULL, failed_logins = 0 WHERE users_id = '".$users_id."'"; $result = mysqli_query($dbc,$query); // Account locked error $errors = true; $message = "Account is unlocked. You may now try to log in again!"; $output = array('errorsExist' => $errors, 'message' => $message); } } Hi, Trying to figure out a way to get a time difference between two times assuming they are in order.. For example; in the array below the days is easy.. because the time happens later, however nights it becomes a little more confusing. The method I'm using now is; if the dates are in order just stick a reference date on there, and get a difference. If they seem to be reversed, I stick a reference date on the first one and a reference date +1 days on the second and get the difference.Can you all think of a better way? I will use the assumption that the times will NEVER be more than 24 hrs apart..
Array ( [2] => Array ( [days] => Array ( [0] => 07:00:00 [1] => 15:45:00 ) [nights] => Array ( [0] => 15:30:00 [1] => 02:15:00 ) )
hello, im trying to figure out the difference between 2 numbers in minutes. here is what i have: Code: [Select] $tsignin="09-05 10:30:00"; $tsignina="09-06 11:30:00"; $log_in_time_stringsa = strtotime($tsignin); $log_out_time_stringsa = strtotime($tsignina); $difference_in_secondssa = ($log_out_time_stringsa - $log_in_time_stringsa); $tsigna = ($difference_in_secondssa / 60); $tsigna2 = number_format(round($tsigna)); the 09-05 is sept 5, and 09-06 is sept 6. this works if the month and date are not included. any ideas for me so i can figure out the total difference between date/times? I have two textboxes that inputted time, and I want to get the difference between two time. for example: $IN = 13:35; $OUT = 17:35; $OTHours = ($OUT - $IN); $OTHours = 4.00; and it is correct, but I have a scenario like this: $IN = 21:35; $OUT = 05:35; $OTHours = -16.00; it should be 8.00. Any help is highly appreciated. Thank you.. What is difference between php date(); and mysql NOW() and what should be used in script? In php date I have 13:00h, and from MySql have 17:00h. Current date on my comp is 19:00h. So time difference in php is +6 hours, and in mysql is +2 hours. How to synchronize this dates for users??? PHP date and time function is not showing correct time on my local system I have the following php code date_default_timezone_set("Africa/Lagos"); $date = date('d-m-y h:i:s'); echo "Server Time ".$date ."<br>"; echo "The time is " . date("h:i:sa")."<br>"; $current_datetime = date("Y-m-d") . ' ' . date("H:i:s", STRTOTIME(date('h:i:sa'))); echo "Current time1: ".$current_datetime . "<br>";
Output
Server Time 21-05-21 09:55:39
Expected Output
Server Time 21-05-21 10:55:39
Any help would be appreciated. Edited May 21 by Ponel This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=310181.0 if($_POST['Submit']=="Check"){ $issueid=(int)$_POST['issueid']; $returndate=mysql_real_escape_string($_POST['returndate']); list($temp)= mysql_fetch_row(mysql_query("select issueid FROM issue where issueid='$issueid'")); if($temp!=NULL){ $insert_query="insert into returning values($issueid,'$returndate')"; $result=mysql_query($insert_query,$linkID1); if($result){ list($bookid,$account,$issuedate,$duedate)= mysql_fetch_row(mysql_query("select bookid,account,issuedate,duedate FROM issue where issueid='$issueid'")); list($title,$name,$fine)= mysql_fetch_row(mysql_query("select title,name,fine FROM book,user where bookid='$bookid' AND acc='$account'")); $insert_book="update book set copies=copies+1 where bookid='$bookid'"; $result=mysql_query($update_book,$linkID1); Quote i want to take difference of $duedate and $returndate and store it to another variable checks that variable for number of days in it Hi. Need some help on one simple task yet so hard. I have puzzled on this for days and no luck. I have a great script i got from somewhere. Code: [Select] function get_time_difference( $start, $end ) { $uts['start'] = strtotime( $start ); $uts['end'] = strtotime( $end ); if( $uts['start']!==-1 && $uts['end']!==-1 ) { if( $uts['end'] >= $uts['start'] ) { $diff = $uts['end'] - $uts['start']; if( $days=intval((floor($diff/86400))) ) $diff = $diff % 86400; if( $hours=intval((floor($diff/3600))) ) $diff = $diff % 3600; if( $minutes=intval((floor($diff/60))) ) $diff = $diff % 60; $diff = intval( $diff ); return( array('days'=>$days, 'hours'=>$hours, 'minutes'=>$minutes, 'seconds'=>$diff) ); } else { trigger_error( "Ending date/time is earlier than the start date/time", E_USER_WARNING ); } } else { trigger_error( "Invalid date/time data detected", E_USER_WARNING ); } return( false ); } Now the other part is that i have 3 textboxes and i woul like to add the first one like 15:00:00 the second one 18:30:00 and the third would display the difference in lets say seconds. At least the function displays the results in seconds. But the problem is how to get the textbox to work whitout posting the info but to use onchange Any ideas? Code: [Select] <input type="text" value="" name="start_time"/> <input type="text" value="" name="end_time"/> <input type="text" value="______" name="difference"/> I have date stored in database in any of the given forms 2020-06-01, 2020-05-01 or 2019-04-01 I want to compare the old date with current date 2020-06-14 And the result should be in days. Any help please? PS: I want to do it on php side. but if its possible to do on database side (I am using myslq) please share both ways🙂 Edited June 14, 2020 by 684425I am having a problem with PHP displaying the correct date and time. It updates as it should, but is fast by 4min and is always displaying a date in 2004. I ran a basic php script to make sure the application im using itself is not wrong. go to lunenburgledger.com/time.php Anybody had any ideas on where to check? The system time on the Windows Server 2003 is correct. The only thing I can think of is that it was converted to a virtual machine on vmware esxi, but the system time stayed right. Any ideas? Thanks! OK So I've got a datepicker that sends a date in d/m/y format. My DB stores the data in Unix Timestamp Which I can convert the date to with strtotime however this does the exact date & time. All I want is the actual day. I've spent hours trying to convert this with just the day with mixed results... Thanks. What is the best way to insert this time stamp into mysql? This is the info I want... Example: Sunday, 28.1.11, 9:56 How would I also select time from the database using mysql? I have a field set as datetime and want to compare that field to see if it is older then ten minutes. Can anyone help I am having a brain fart? Hi! The below dropdown will show dates/times every 15 minutes from 12am to 11:45pm for only 1 day. I want it to show for 5 days, anyway I can do this? Below code: Code: [Select] $start = strtotime('12:00am'); $end = strtotime('11:45pm'); echo '<select name="time">'; for ($i = $start; $i <= $end; $i += 900) { echo '<option>' . date('F j, Y, \a\t g:i a', $i); } echo '</select>'; Thanks for any help. |