How to calculate the time difference between two days in seconds - php

i have a php page .
Here i will get the start time as query string (eg $startTime= '8/27/2010 1:15:17 PM')
Now i need to find out the total seconds between the current time and started time
How can i find out this in php

echotime()-strtotime($startTime);

Related

Retrieve remaining time between two dates considering business hours

I am trying to write a php solution to calculate the planned end time considering the target in business hours.
It shouldn't consider some days (retrieved from setting saved in db) such as holidays.
Also business hours are retrieved from db (morning_from (8:30am), morning_to (1:00pm), evening_from (2:30pm), evening_to (6:30pm)).
I want to develop this script because I want that my page shows the remaining time for technical resolution of an opened ticket every day.
For example:
customer having contract with 10 working hours SLA opens a ticket
today (friday) 31/01/2020 16:00:00, considering that in the
noBusinessDays = array("saturday", "sunday") and businessHours set as mentioned before(8:30-13:00/14:30-18:30), the result will have to
be monday 3/02/2020 17:30:00.
Code example:
$noBusinessDays = array("saturday", "sunday");
$businessHours = array("morning_from" => "8:30", "morning_to" => "13:00", "evening_from" => "14:30", "evening_to" => "18:30");
$SLA = "10"; //hours
$ticketDate = new DateTime();
$ticketDate->setTimestamp(strtotime("31/01/2020 16:00:00"));
// I don't know how to use my arrays to say in this calculation how to use them
$maximumLimit = $ticketDate->add(new DateInterval("PT" . $SLA ."H"));
Thank you in advance.
You may use the following function
// intersection of 2 time intervals
// input - Unix timestamps (start,end)
// output - intersection in seconds
function time_union($b_1,$e_1,$b_2,$e_2)
{
return max(0,$e_1-$b_1 - max(0,$e_1-$e_2) - max(0,$b_2-$b_1));
}
You will start with an empty time interval [X, Y) where X is the timestamp of the ticket creation and Y initially is equal to X.
Then you start adding days to Y - one by one. Each time you expand the time interval to contain another day - you use the above function to check how much of the SLA hours are covered (i.e. overlapping) with the working hours in the day you have just added. If the day is marked as a holiday - you simple skip it and continue with the next date.
If you find out that SLA hours are partially covered with either the morning or evening business hours - you should simply subtract the extra hours.
In the end Y will be equal to the timestamp that you want to show in your application.
I think I'd break down the problem into pieces. After calculating the total number of days in the interval, first dispose of the trivial case that it's all happening in one week.
begin by calculating the number of "whole weeks." Each "whole week" is five business days. Subtract the corresponding interval of time and proceed. Now, look at the day-of-the-week of the start-date: each day adds a certain number of days. Then the day-of-week of the end date, likewise. You can then consider hour-of-the-day as needed.
Holidays are a simple table: if the day falls within the range, subtract one day.
Now ... having said all of that, the very first thing that I would do is to search GitHub and SourceForge! Because I am extremely sure that somebody out there has already done this. :-D
"Actum Ne Agas: Do Not Do A Thing Already Done."

PHP - Get a time interval of an interval

I have been trying to use the DateTime class to calculate an interval.
I use the unix timestamp from $_SERVER['REQUEST_TIME'] and a time retrieved from the database to get an interval, which is of time DateInterval. That part works.
That interval could be, for example, 5 minutes and 10 seconds since a process began.
My expected time for that process to complete is 10 minutes, and I want to inform the user that there is 4 minutes, 50 seconds expected time left.
$running_time; // calculated before, type DateInterval. Echoing the formatted value shows correct time
// (only problem is no leading 0's for single-digit minutes/seconds in format: '%i:%s')
$total = new DateTime('10:00'); // ten minutes, 0 seconds expected total running time
$time_left = $total->diff(new DateTime($running_time->format('%i:%s'))); // fatal error
I think this would work, except that, oddly, the format function is not working as expected. The minutes and seconds do not have leading zeros when they are single digits.
Any hints?
Yeah, the PHP documentation is off on this one.
Capitalize your I and S and you'll get your leading zeroes.
$running_time->format('%I:%S')

PHP DateTime Subtraction and Conversion

It's hard to articulate the problem I'm having in the title, but here's the scenario: I'm working the backend of a 3-tiered exam taking website. I'm currently updating my function used for when students request an exam. The exam has start and end dates and times, as well as a time limit. What I want to do is specific to the case when the amount of time left in the exam period is less than the time limit for the exam, For example, the exam period ends at 5:00PM. The time is now 4:30PM. The exam time limit is an hour and a half. I want to replace the hour and a half time limit with 30 minutes. To do that, there are 3 dates I need, and I will explain how I get each:
Timestamp for my function page. When the controller calls my page, after I open the session, I set the default timezone to America/New York (my region). To get the timestamp, I use the code:
$date = date('Y-m-d H:i:s');
$currenttime = date('H:i:s', strtotime($date));
I use a function call to my database to acquire the time limit and the time the exam ends (I also use the dates for other comparisons to check exam availability). The exam end time is stored in resultant array $res[10] and the time limit is in $res[11].
Assuming the due date for the exam is equal to the date of the timestamp, and it is in the exam period (2 checks I perform before), I want to check if the difference between the current time and the end of the exam is less than the time limit. If the time until the exam ends is less than the time limit, I want to set that amount of time as the new time limit. My front end expects my result in the form HH:mm:ss (php's equivalent is H:i:s). So if there is 30 minutes until the exam ends and the time limit is an hour and a half, I want the new time limit to be 30 minutes (or 00:30:00). My code is currently not working as desired. I am meeting the criteria for the if statement, which is my desired result. When I am converting it to the form H:i:s is where the problem seems to be (I keep getting a result around 19:00:00). Every article I've read suggests to do it this way, but I may be missing something.
$compareTime = strtotime($res[10]) - strtotime($currenttime);
if(compareTime < strtotime($res[11]){
$timelimit = date('H:i:s', $compareTime);
}else{
$timelimit = $res[11];
}
If there is a better way of doing this, I'd be much obliged to know. I'm fairly new to php, and admittedly I realize this may be a strange problem, but that's the way the group decided to go.
Maybe it's because the databse and PHP server are using different timezones. Check it out by printing these variables to see their values.

Add multiple timestamps together from mysql db then get hours and minutes

What's the best way to retrieve start_time and end_time times from a mysql db using php and add them together to get a total of hours and minutes? An example military time in my db is: 18:35:19 to 22:05:14. Is it easier with standard or military time? Thanks in advance.
Carlos,
Some further information such format the data is stored within mysql would have been helpful, as there are certain mysql queries to help with this. But seeing as you've tagged PHP, I would do the following in php:
convert the time to unixtimestamp using something like mktime (http://php.net/manual/en/function.mktime.php)
$unixtimestamp_start = mktime($hour,$minute,$second,$month,$day,$year);
$unixtimestamp_finish = mktime($hour,$minute,$second,$month,$day,$year);
minus the start time from the finish time.
$timeDiffSeconds = $unixtimestamp_finish - $unixtimestamp_start;
You now have the amount of seconds between finish and start.
you can divide that by 60 to have minutes
$timeDiffMins = $timeDiffSeconds/60;
you can divide that by 60 again to have hours.
$timeDiffHrs = $timediffMin/60;

PHP function that will turn a time stamp in the format yyyy-mm-dd hh:mm:ss into how old the time stamp is

Basically, I have a time stamp that looks like 2010-12-15 16:14:06. If the time stamp is seconds old I want it transform it into "N Seconds Old." If the time stamp is minutes old, I want it to transform it into "N Minutes Old." etc. Is there any built in PHP functions that will do this?
if you're running 5.3 you can use DateTime::diff to do it. It returns a DateInterval object which contains the info you'll want.
As mentioned by others, you need to have some "starting point" to measure against. There are a number of ways to turn a time stamp such as 2010-12-15 16:14:06 into a PHP timestamp (seconds since 1970-01-01 00:00:00). If that's your "starting point", simply subtract it from the current time or whatever to get the difference in seconds. If less than a certain amount (say, 60), give that as "N seconds ago". If greater, divide by 60 to get minutes ago, or 3600 to get hours ago, etc.).

Categories