PHP - Php Time (not Timedate) Difference
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 ) )
Similar Tutorialsi 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"; 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, 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. 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 ? 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? 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. Hey 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. 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.. This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=310181.0 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"/> Ok I got this column "date_reg" on my table which has a "datetime" type. It's giving me a value like "2012-04-20 04:28:03" The problem is, when I'm trying to insert on that column using this format Code: [Select] <?php $datenow = date('m/d/Y h:i:s a', time()); ?> it's giving me a value of "0000-00-00 00:00:00" which is suppose to be the current date and time the user add on that table. Anyone knows how I can fix this? 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 I am trying to simulate an ad expiration and carry out an action if the ad is expired. And I cannot get the if/else to work properly... I've tried many variations and I cannot see what I am doing wrong here. Any tips please 3 hours and counting of no solution! $ad_start = time()-14 . "<br />"; // 14 days from today in the past (negative) echo $ad_start; $current_time = time() . "<br />"; // current epoch time echo $current_time; $days_past = $ad_start - $current_time; // days past echo "<br />$days_past days have past since the ad started!<br />"; if($days_past <= 14) { echo "<br />Ad is less than 14 days. Not expired."; } else { echo "<br />Ad is over 14 days. Expired."; } Basically I have recently been playing around with parsing a csv file. What I am looking to do at this point is simply take the date/timestamp (part of the csv file), which is in the following format:DD/MM HH:MM:SS.100th/s For the sake of argument, lets say I have this in an array string called $csv[0] and the file has several lines that span the course of a couple hours. I wouldn't mind having to use explode() to breakup/remove the date or 100th/s IF that would make things a lot simpler. So where would I start in trying to achieve this?. The result I am looking for will simply return "X Seconds". Storing this in a string variable would be a bonus, as I plan to use this to divide a separate piece of information. Any examples or ideas would be great. Thank you. ps: Here is an example time from the csv file itself: Code: [Select] 11/19 22:23:18.143 I 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 are the differences and implications of UTC time and Zulu time? <?php function getArr(string $time):array { $dateTime = new \DateTime($time); return [ 'time'=>$time, 'timestamp'=> $dateTime->getTimestamp(), 'dateTime' => $dateTime ]; } $arr = getArr('2020-08-05'); $arr_z = getArr('2020-08-05T00:00:00Z'); print_r($arr); print_r($arr_z); echo('equal timestamps: '.($arr['timestamp'] === $arr['timestamp']?'true':'false'));
Array ( [time] => 2020-08-05 [timestamp] => 1596585600 [dateTime] => DateTime Object ( [date] => 2020-08-05 00:00:00.000000 [timezone_type] => 3 [timezone] => UTC ) ) Array ( [time] => 2020-08-05T00:00:00Z [timestamp] => 1596585600 [dateTime] => DateTime Object ( [date] => 2020-08-05 00:00:00.000000 [timezone_type] => 2 [timezone] => Z ) ) equal timestamps: true
Hello, I tried to implement some PHP code to enable a web page to show "Dinner" specials versus "Lunch" specials based on the time. The business is located in the Eastern Time Zone, so I only need to routine to work based on Eastern Time (New York). What I am noticing is that the server is processing the lines of code so fast that the command to establish the correct time for the remaining code is not always being acknowledged. This line of code appears to be processing too fast for the remaining lines of code: date_default_timezone_set ( "America/New_York" ); Is there some additional code I can put in place to make sure the correct time is always ascertained? I need the $hourmin variable to always return a combination of hour + minute based on 24-hour time and Eastern Time zone. My code is as follows: <?php $name8 = file_get_contents("test/special8name.txt"); date_default_timezone_set ( "America/New_York" ); $gethour = date("H"); $getminutes = $gettimedate["minutes"]; $gettimedate = date(); $hourmin = $gethour . $getminutes; $currentday = date("l", time()); $currentdate = date("M j, Y"); if ($hourmin < 1500 && $currentday <> "Saturday" && $currentday <> "Sunday") { echo "<span class=\"namesred\">$name8 </span>"; } else if ( $hourmin > 1500 && $hourmin < 2300 && $currentday <> "Saturday" && $currentday <> "Sunday") { echo "<span class=\"namesblue\">$name8 </span>"; } else if ( $currentday == "Saturday" or $currentday == "Sunday") { echo "<span class=\"namesblue\">$name8 </span>"; } ?> PHP 5.2.6 We rent PHP server space on a server in California. Several of my pages generated by PHP show the current date and time, so when the page is printed, the user knows when the data was printed. Our users could be anywhere in the US, I am in Michigan. How do I convert the server time (PDT) into local time, regardless of where the user is? Thanks. |