Im trying to solve this since two days without success...
First of all my code:
PHP-Version: 5.4
SCRIPT1:
query = "SELECT start, end FROM timetable WHERE ........";
$result = mysql_query($query, $db) or die(mysql_error());
$sqlarr = mysql_fetch_array($result, MYSQL_ASSOC);
$times = array($sqlarr['start'], $sqlarr['end']);
$calced = strtotime($times[1]) - strtotime($times[0]);
$total = date("H:i:s", $calced-3600); //<-- -3600 Fixed it
echo "<br>Total: ".$total;
The start and end times are in format 00:00:00. Everytime this script calculates it appends 1 hour to the result. So if im going for a result of 5 minutes i´ll get 01:00:05.
Why???
This one is even more strange.
SCRIPT2:
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo " | ".$row['total']."<br>";
$add += strtotime($row['total']);}
echo $add;
The first script calculates the total time from start to end. The second one should get the total-times out of the db and calculate the sum of all entries. For every entry the second script substracts 2 hours.
Example:
Database => Start = 12:32:00 End = 12:32:15
Script1 Result1 = 01:00:15 (Where is this extra hour coming from?) FIXED
Every Result1 is stored in the same table(db). Script2 is loading all this rows and handling them by a while-loop.
According to how many entries there are the script subtracts serval hours.
0 Entries => Total: 00:00:00
1 Entry => Total: 23:00:xx
2 Entries => Total: 20:00:xx
3 Entries => Total: 18:00:xx
4 Entries => Total: 16:00:xx
So, with 2 entries it continues subtracting 2 hours from every total-calculation which isnt correct, abviously.
Thanks to you guys. Using DateTime made this simple and bugfree!
I am not really sure of your case with using strtotime function, but I've tried this solution and it worked, if you have php > 5.2:
You can use the DateTime class and date_diff function to get the difference date, read more here: http://us3.php.net/manual/en/datetime.diff.php
Example:
// start date
$start = new DateTime("09:23:38");
// end date
$end = new DateTime("09:23:54");
// calculate difference
$calc = date_diff($start, $end);
// prints 00:00:16
echo $calc->format('%h:%i:%s');
According to this answer It seems like by default date starts from 1:00:00 so you could subtract your date by 3600 seconds (1 hour) like this:
$start = strtotime("11:23:38");
$end = strtotime("11:23:54");
$calc = $end - $start;
echo date('H:i:s', $calc - 3600);
The problem comes from :
echo date('H:i:s', $calc);
$start - $end gives you the expected value in seconds. It depends on what value date('H:i:s', 0) is. On my computer, it's not midnight on 1 jan. 1970 !
This link as example : http://sandbox.onlinephpfunctions.com/code/81e56cbce16f6dacfedd2cb376b946a540bce36d.
You might consider substracting date('H:i:s', 0) from your result to get correct value.
Or using DateTime as #Ben Beri suggested you to do as it is (I think) the best way to do.
Related
I am working on a project and writing a function to add two different times. The times are stored in database as a string.
I'm:
Pulling value from db
converting it into time using strtotime
adding times using date function
Here is my code:
$time_1 = '1:00';
$time_2 = '0:05';
//should be 1:05, whereas it prints 04:05
echo date("H:i", strtotime($time_1) + strtotime($time_2));
Please tell me, what is wrong with above code and how it can be fixed?
Thanks
Your problem is because strtotime returns the number of seconds since the Unix Epoch (Jan 1 1970). So what you are getting is not values of 60 and 5, but something more like 1537570800 and 1537567500. When you add those two values together, you end up with a date far in the future, with what looks effectively like a random time. To compensate for this, you need to subtract the value of strtotime at the start of the day to make the second time a relative time e.g.:
echo date("H:i", strtotime($time_1) + strtotime($time_2) - strtotime('00:00'));
Output:
01:05
Update
Since it turns out that the sum of the two times can exceed 24 hours, the above code will not work (the maximum time it will display is 23:59 before rolling over to 00:00. So it is necessary to convert both times to a relative number of minutes to the start of the day, add them and then display as hours and minutes:
$time_1 = '12:00';
$time_2 = '14:30';
$time_sum = (strtotime($time_1) + strtotime($time_2) - 2 * strtotime('00:00')) / 60;
printf('%02d:%02d', intdiv($time_sum, 60), $time_sum % 60);
Output:
26:30
Use DateTime::createFromFormat function, and taking ideas from Adding two DateTime objects in php
$time_1 = '1:00';
$time_2 = '0:05';
$t1 = DateTime::createFromFormat('G:i', $time_1);
$t2 = DateTime::createFromFormat('G:i', $time_2);
$interval1 = $t1->diff(new DateTime('00:00:00')) ;
$interval2 = $t2->diff(new DateTime('00:00:00')) ;
$e = new DateTime('00:00');
$f = clone $e;
$e->add($interval1);
$e->add($interval2);
$total = $f->diff($e)->format("%H:%I:%S");
Additional Details:
G and H 24-hour format of an hour with or without leading zeros
i Minutes with leading zeros 00 to 59
I have a form where I ask 4 hours, It's morning start and end hour and the same for afternoon.
I'm using Carbon but it returns me 0 if I put 30 minutes, it rounds to down.
I try with diffInHours and diffInMinutes but it rounds always down in both ways.
This is my code example:
$startTime1 = Carbon::createFromFormat('H:s', '09:00');
$startTime2 = Carbon::createFromFormat('H:s', '10:30');
$startTime3 = Carbon::createFromFormat('H:s', '14:00');
$startTime4 = Carbon::createFromFormat('H:s', '16:00');
$morningTime = $startTime2->diffInHours($startTime1);
$afternoonTime = $startTime4->diffInHours($startTime3);
$total = $morningTime + $afternoonTime;
In $morningTime it returns 1 and should return 1,30.
In $afternoonTime it returns 2 and it's correct.
If I use with diffInMinutes it put 60 in $morningTime and 120 in $afternoonTime. It's wrong again in morning time.
How can I solve that?
Thank you
You are assigning seconds H:s in the Carbon format, use H:i:
$startTime1 = Carbon::createFromFormat('H:i', '10:30');
And You can use format() method to format the difference in time
$morningTime = $startTime2->diff($startTime1)->format('%H:%I');
Edit: there is no way to combine dateInterval instance, but you still can use timestamp:
$total_in_seconds = ($startTime2->timestamp - $startTime1->timestamp) + ($startTime4->timestamp - $startTime3->timestamp);
$total = Carbon::createFromTimestamp($total_in_seconds)->format('h:i');
I have to make a notification after 30 days.
foreach ($pdo->query($sql) as $row) {
$date = date_create($row['data']);
$laikotarpas = date_diff(new DateTime("now"), $date);
// var_dump($liko);
$liko = 30 - $laikotarpas->d;
I want correct result in days.
I have added a row at 2014.03.19 and this shows that left 3days to 30.
My goal is to achieve:
I add record at 2014.03.19 and get result how many days have passed from today. I thought that $laikotarpas->d gives a duration in days, but, when i do calculations to set the limit for 30days.
So my main problem is to get correct $liko, but I have no idea how.
I am adding my time using this code (using PDO):
$q->execute(array($name,
date("Y-m-d H:i:s", time())
);
In my database I use DATETIME. And i print that date from SQL using this php:
<?php echo date_format(date_create($data['data']), 'Y-m-d'); ?>
Is my way good? How to improve this?
-----edit-----
I have to use php5.2
Just got an idea, it takes only days and ignores months passed count. How to update that to count duration only in days?
If you want to find the difference between now and a date in the past, try something like this:
PHP >= 5.2.0
$then = '2014-03-19';
$date = new DateTime($then);
$now = new DateTime('now');
$diff = $date->diff($now);
echo $diff->days . ' days since ' . $then . PHP_EOL; // 58 days since 2014-03-19
PHP < 5.2.0
$date = strtotime($then);
$now = time();
$diff = $now - $date;
$days = round($diff / 60 / 60 / 24); // convert seconds to days and round off
Note: after understanding more about your problem, I highly suggest you filter your results based on date ranges in MySQL rather than PHP - it'll be easier and more economic and will reduce your potential risk for affecting data you didn't mean to. See Cull Larson's answer.
You could just use a query like this:
SELECT * FROM myTable WHERE DATE(signupDate) = DATE_SUB(NOW(), INTERVAL 25 DAY);
That will give you all results with a signup date that is 25 days old. If you have a flag in the table telling you whether you've notified them, you can pass that along too:
SELECT * FROM myTable WHERE notified=false AND DATE(signupDate) = DATE_SUB(NOW(), INTERVAL 25 DAY);
If you want to get every record 25 days or older, that hasn't been notified:
SELECT * FROM myTable WHERE notified=false AND DATE(signupDate) <= DATE_SUB(NOW(), INTERVAL 25 DAY);
I wrote the following code to determine the amount of time that employees spend on a task:
$time1 = $row_TicketRS['OpenTime'];
$time2= $row_TicketRS['CloseTime'];
$t1=strtotime($time1);
$t2=strtotime($time2);
$end=strtotime(143000); //143000 is reference to 14:30
//$Hours =floor((($t2 - $t1)/60)/60);
$Hours = floor((($end- $t1)/60)/60);
echo $Hours.' Hours ';
The above code is not giving me the correct time.
For example, with a start time of 09:19:00 and end time of 11:01:00 it give me duration time of only 1 hour which is wrong. What is the correct way?
Your use of floor is why you are getting only 1 hour for those inputs. Those inputs result in 1.7 hours if you keep the answer as a float. floor automatically rounds down to the lower integer value. Check out http://php.net/manual/en/function.floor.php for more info.
$t1 = strtotime('09:19:00');
$t2 = strtotime('11:01:00');
$hours = ($t2 - $t1)/3600; //$hours = 1.7
If you want a more fine-grained time difference, you can flesh it out...
echo floor($hours) . ':' . ( ($hours-floor($hours)) * 60 ); // Outputs "1:42"
UPDATE:
I just noted your comments on Long Ears' answer. Please check my comments above again, they are correct. Inputting values of '09:11:00' and '09:33:00' results in 0 hours (22 minutes).
If you input those values and got 4 hours, you likely have a decimal error in your math. Using '09:11' to '09:33', the result is .367 hours. If you divided the strtotime results by 360 instead of by 3600, you would get result 3.67 hours (or 4 hours, depending on your rounding method).
strtotime converts your time to an int value representing number of seconds since Unix epoch. Since you convert both values to seconds, and then subtract the values from each other, the resulting value is a quantity of seconds. There are 3600 seconds in 1 hour.
After changing strtotime('14:30:00') everything working fine.. see below
$time1 = '09:19:00';
$time2= '11:01:00';
echo "Time1:".$t1=strtotime($time1);
echo "<br/>Time2:".$t2=strtotime($time2);
echo "<br/>End:".$end=strtotime('14:30:00');
echo "<br/>Floor value:";
var_dump(floor((($end- $t1)/60)/60));
//$Hours =floor((($t2 - $t1)/60)/60);
$Hours = floor((($end- $t1)/60)/60);
echo $Hours.' Hours ';
function getTimeDiff($dtime,$atime)
{
$nextDay=$dtime>$atime?1:0;
$dep=explode(':',$dtime);
$arr=explode(':',$atime);
$diff=abs(mktime($dep[0],$dep[1],0,date('n'),date('j'),date('y'))-mktime($arr[0],$arr[1],0,date('n'),date('j')+$nextDay,date('y')));
//Hour
$hours=floor($diff/(60*60));
//Minute
$mins=floor(($diff-($hours*60*60))/(60));
//Second
$secs=floor(($diff-(($hours*60*60)+($mins*60))));
if(strlen($hours)<2)
{
$hours="0".$hours;
}
if(strlen($mins)<2)
{
$mins="0".$mins;
}
if(strlen($secs)<2)
{
$secs="0".$secs;
}
return $hours.':'.$mins.':'.$secs;
}
echo getTimeDiff("23:30","01:30");
A better way is to use http://php.net/manual/en/datetime.diff.php
$start_t = new DateTime($start_time);
$current_t = new DateTime($current_time);
$difference = $start_t ->diff($current_t );
$return_time = $difference ->format('%H:%I:%S');
for example the start time is 09:19:00 and end time is 11:01:00 but it give me duration time only 1 hour which is wrong
You are calculating the difference in hours. what is the correct result for "start time is 09:19:00 and end time is 11:01:00"
You need strtotime('14:30') rather than strtotime(143000)
Edit: Actually to my surprise, strtotime(143000) does seem to have the desired effect but only for double-digit hours so I still wouldn't rely on it. Anyway it's not the cause of your problem ;)
You can use $hour = ($end - $t1)/(60*60)
In this the time format is (seconds*minutes*days*months*years) => (60*60*2)
Suppose the target time is 4.30 pm and the current time is 3.25 pm , how will i calculate the minutes remaining to reach the target time ? I need the result in minutes.
session_start();
$m=30;
//unset($_SESSION['starttime']);
if(!$_SESSION['starttime']){
$_SESSION['starttime']=date('Y-m-d h:i:s');
}
$stime=strtotime($_SESSION['starttime']);
$ttime=strtotime((date('Y-m-d h:i:s',strtotime("+$m minutes"))));-->Here I want to calcuate the target time; the time is session + 30 minutes. How will i do that
echo round(abs($ttime-$stime)/60);
Krishnik
A quick calculation of the difference between two times can be done like this:
$start = strtotime("4:30");
$stop = strtotime("6:30");
$diff = ($stop - $start); //Diff in seconds
echo $diff/3600; //Return 2 hours. Divide by something else to get in mins etc.
Edit*
Might as well add the answer to your problem too:
$start = strtotime("3:25");
$stop = strtotime("4:30");
$diff = ($stop - $start);
echo $diff/60; //Echoes 65 min
Oh and one more edit:) If the times are diffent dates, like start is 23:45 one day and end is 0:30 the next you need to add a date too to the strtotime.