This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get time difference in minutes in PHP
I am working on an attendance table to calculate the late and very late employees.
I am storing the login time in the table (Type: time). I am able to get the time from the database and i would like to show the time difference in the separate column.
i.e., if employee logged on or before 09:00:59, then its right time and the time diff should be shown as null. If he logs in after the time i.e, 09:01:00 or later the time difference should be 00:00:01. Like wise i need to calculate the differences in time.
One time is constant i.e., 09:00:59 and another one i am getting from database table. Need to get diff between both.
I am working in PHP.
Hope my question is clear.
Thank you in Advance.
You can use strtotime() for time calculation. Here is an example:
$checkTime = strtotime('09:00:59');
echo 'Check Time : '.date('H:i:s', $checkTime);
echo '<hr>';
$loginTime = strtotime('09:01:00');
$diff = $checkTime - $loginTime;
echo 'Login Time : '.date('H:i:s', $loginTime).'<br>';
echo ($diff < 0)? 'Late!' : 'Right time!'; echo '<br>';
echo 'Time diff in sec: '.abs($diff);
echo '<hr>';
$loginTime = strtotime('09:00:59');
$diff = $checkTime - $loginTime;
echo 'Login Time : '.date('H:i:s', $loginTime).'<br>';
echo ($diff < 0)? 'Late!' : 'Right time!';
echo '<hr>';
$loginTime = strtotime('09:00:00');
$diff = $checkTime - $loginTime;
echo 'Login Time : '.date('H:i:s', $loginTime).'<br>';
echo ($diff < 0)? 'Late!' : 'Right time!';
Demo
Check the already-asked question - how to get time difference in minutes:
Subtract the past-most one from the future-most one and divide by 60.
Times are done in unix format so they're just a big number showing the
number of seconds from January 1 1970 00:00:00 GMT
You can also use DateTime class:
$time1 = new DateTime('09:00:59');
$time2 = new DateTime('09:01:00');
$interval = $time1->diff($time2);
echo $interval->format('%s second(s)');
Result:
1 second(s)
<?php
$start = strtotime("12:00");
$end = // Run query to get datetime value from db
$elapsed = $end - $start;
echo date("H:i", $elapsed);
?>
Related
This question already has answers here:
AM/PM comparison in PHP [duplicate]
(3 answers)
Closed 4 months ago.
time() function in PHP used to get current time.But there's a problem!
Suppose we get the current time 12:05 AM (EST) using this function, then if i compare this time with another time(For example,10:50 AM),obviously PHP will consider that the timestamp of 10:50 AM is greater than 12:05 AM)..but actually it's wrong!12:05 is basically 00:05 AM...!!!
I don't know how to compare times like this accurately,any help would be appreciated.
I'm using this block of code to compare those two times...
$current_time = time(); //suppose current time is 12:15 AM(returned by time() func)
$time1 = '1:30 AM';
$draw_time = strtotime($time1);
if($current_time < $draw_time1) {
echo 'Perfectos!';
}
$current_time = time();
$time1 = date('d.m.Y') . ' 11:42 AM';
$draw_time = strtotime($time1);
echo ($draw_time) . " " . $current_time . "\r\n";
if ($current_time < $draw_time) {
echo 'True';
} else {
echo 'False';
}
You can try this way
Your code is incorrect , bacause you get first variable for only hour and second variable for whole time
This question already has answers here:
Print time in 15-minute increments between two times in the same day
(10 answers)
Closed 11 months ago.
I'm trying to echo list of hours.
<?php
$startTime = "9:00:00";
$endTime = "19:00:00";
$st = strtotime($startTime);
$et = strtotime($endTime);
while ($st < $et) {
echo $st = date( "H:i", strtotime('+30 minutes', $st) );
echo '<br>';
}
?>
Inside that list, users will choose their reservation hour. But my while loop works like infinite loop.
Just like the previous answer, you're messing with data types while doing arithmetic.
PHP ships with built-in date time arithmetic through date and time API.
Your code could look much more clear and elegant and less error prone (no strtotime(), conversion, etc)
You could do something like this instead:
<?php
$startTime = DateTime::createFromFormat('H:i:s', '9:00:00');
$endTime = DateTimeImmutable::createFromFormat('H:i:s', '19:00:00');
$interval = new DateInterval('PT30M'); // 30 minutes
$reservation = [];
while ($startTime <= $endTime) {
$reservation[] = $startTime->format('H:i');
$startTime->add($interval);
}
echo join(PHP_EOL, $reservation) . PHP_EOL;
Links:
Date/Time Arithmetic
You're converting $st from a integer (timestamp) with strtotime() to a string with date() so your loop keeps going since while (string < integer)... is always true.
You can do this instead:
while ($st < $et) {
$st += (30 * 60); // Add 30 minutes in seconds
echo date("H:i", $st);
echo '<br>';
}
To convert a date to timestamp, I usually do this- strtotime ("2018-05-17 05:04:34) but now, I want to convert just hours (without date) e.g. 02:00:00 to timestamp. How do I do this?
Why I need this is to compare if a certain time is greater than the hour specified. This is what I am doing:
$reported = strtotime("2018-05-17 05:04:34");
$respons = strtotime("2018-05-17 17:04:34);
$response_time = $respons - $reported;
I want to be to check if $response_time is greater than 1 hour.
There is DateTime::diff, which probably does what you need
https://secure.php.net/manual/de/datetime.diff.php
In your case that should be
$datetime1 = new DateTime("2018-05-17 05:04:34");
$datetime2 = new DateTime("2018-05-17 17:04:34);
$interval = $datetime1->diff($datetime2);
echo $interval->format('H hours');
Strtotime has no problem parsing a time without a date.
No need to fake a date which will come back and bite you with daylight savings.
I also added a check to see if the start/end is "reversed".
$start= "2018-05-17 05:04:34";
$end = "2018-05-17 17:04:34";
//Note that it's intentionally reversed
$diff = strtotime(substr($start,11))-strtotime(substr($end,11));
//If the calculation was reversed add one day in seconds
if($diff <0) $diff += 86400;
If($diff >3600){
Echo "more than one hour";
}Else{
Echo "less than one hour";
}
https://3v4l.org/g1jZH
I believe only I have understood your question correctly.
I want to convert just hours (without date) e.g. 02:00:00 to timestamp.
There's no Date component here.
Okay, I assume they are of same date. If that's the case, just append an arbitrary date in front of the two to make the strtotime() function work:
$start = "05:04:34";
$end = "17:04:34";
$reported = strtotime("2018-05-17 " . $start);
$respons = strtotime("2018-05-17 " . $end);
$response_time = $respons - $reported;
if ($response_time > 3600)
echo "More than hour!";
else
echo "Less than hour!";
Note: This doesn't work if the start time is 17:00 and end time is say, 08:00 - which occurs in the next day. You have to make sure if the start time is greater than end time, then you have to add one more day to the end time.
I like the DateTime class, give this a try:
<?php
$reported = new DateTime('2018-05-17 05:04:34');
$reported->modify('+2 hours');
$now = new DateTime();
echo $now < $reported ? 'less than 2 hours' : 'more than 2 hours';
See it here https://3v4l.org/T7BEL
See the DateTime class docs here http://php.net/manual/en/class.datetime.php
Let's say I got this time 21:07:35 now and this time into a variable 21:02:37 like this
<?php
$current_time = "21:07:35";
$passed_time = "21:02:37";
?>
Now I want check if $current_time is less than 5 minutes then echo You are online
So how can I do this in PHP?
Thanks
:)
To compare a given time to the current time:
if (strtotime($given_time) >= time()+300) echo "You are online";
300 is the difference in seconds that you want to check. In this case, 5 minutes times 60 seconds.
If you want to compare two arbitrary times, use:
if (strtotime($timeA) >= strtotime($timeB)+300) echo "You are online";
Be aware: this will fail if the times are on different dates, such as 23:58 Friday and 00:03 Saturday, since you're only passing the time as a variable. You'd be better off storing and comparing the Unix timestamps to begin with.
$difference = strtotime( $current_time ) - strtotime( $passed_time );
Now $difference holds the difference in time in seconds, so just divide by 60 to get the difference in minutes.
Use Datetime class
//use new DateTime('now') for current
$current_time = new DateTime('2013-10-11 21:07:35');
$passed_time = new DateTime('2013-10-11 21:02:37');
$interval = $current_time->diff($passed_time);
$diff = $interval->format("%i%");
if($diff < 5){
echo "online";
}
$my_time = "3:25:00";
$time_diff = strtotime(strftime("%F") . ' ' .$my_time) - time();
if($time_diff < 0)
printf('Time exceeded by %d seconds', -$time_diff);
else
printf('Another %d seconds to go', $time_diff);
I have been looking for an answer for a few hours now, but I can't find one.
I'm writing a simple script. The user sets their work start and end time. So, for example, somebody is working from 8:00 to 16:00.
How can I subtract this time to see how long the person has been working?
I was experimenting with strtotime(); but without success...
A bit nicer is the following:
$a = new DateTime('08:00');
$b = new DateTime('16:00');
$interval = $a->diff($b);
echo $interval->format("%H");
That will give you the difference in hours.
If you get valid date strings, you can use this:
$workingHours = (strtotime($end) - strtotime($start)) / 3600;
This will give you the hours a person has been working.
Another solution would be to go through the Unix-timestamp integer value difference (in seconds).
<?php
$start = strtotime('10-09-2019 12:01:00');
$end = strtotime('12-09-2019 13:16:00');
$hours = intval(($end - $start)/3600);
echo $hours.' hours'; //in hours
//If you want it in minutes, you can divide the difference by 60 instead
$mins = (int)(($end - $start) / 60);
echo $mins.' minutues'.'<br>';
?>
This solution would be a better one if your original dates are stored in Unix-timestamp format.