Helllo, I have 2 time variables. One I take from the database and another is the current time.
$time_ended = $row['time_ended'];
$time_ended = new DateTime($time_ended);
$now = new DateTime();
What I want is to subtract $now from $time_ended and echo the number of seconds in a result.
Basically the output should be something like 121 or 134. I need this data to create a countdown timer.
Can someone show me the trick?
Thank you for your time.
PHP > 5.3: http://php.net/datetime.diff
PHP < 5.3: http://php.net/strtotime
Use the diff method:
// PHP > 5.3
$diff = $time_ended->diff( $now );
echo $diff->format( '%s' ); // Seconds, with leading zeros
Or if you are not running PHP > 5.3:
// PHP < 5.3
$diff = ( strtotime( $now ) - strtotime( $time_ended ) ) / 3600;
echo date( 's', $diff ); // Seconds, with leading zeros
http://php.net/manual/en/datetime.diff.php
Use the diff method of the DateTime object.
$interval = $time_ended->diff($now);
$diff_seconds = $interval->s; // returns the number of seconds
This will return a DateInterval (http://www.php.net/manual/en/class.dateinterval.php) object. The s property gets the number of seconds of the interval.
Related
I am trying to calculate the difference in time between two times using this:
round(abs(strtotime("17:30") - strtotime("18:30")) / 60,2);
= '1'
which works fine, but as soon as i make it over 2 days its not calculating correctly
round(abs(strtotime("17:30") - strtotime("02:00")) / 60,2);
= '15.5' this should be '8.5'
For more accurate and correct results you can use ->diff() function. As an example:
<?php
$val1 = '2014-03-18 10:34:09.939';
$val2 = '2014-03-14 10:34:09.940';
$datetime1 = new DateTime($val1);
$datetime2 = new DateTime($val2);
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>
Output:
-4 days
strtotime returns an timestamp. Currently your calculation is only partly correct, because you ignore negative values. In case of negative values (that should be the case, if the second time is on the next day), you should add 86400 (24*60*60 - the seconds of a day).
$start = strtotime("17:30");
$end = strtotime("02:00");
$diff = $end - $start;
// end date is on the next day
if ($diff < 0) {
$diff += 86400;
}
$hours = $diff / 3600;
echo round($hours, 2);
You can do the math yourself by converting dates into unixtime:
strtotime('2017-12-29 02:00')-strtotime('2017-12-28 17:30')
This will return the difference in seconds, so if I want to print the value in hours, I have to divide by 60 twice:
php > print((strtotime('2017-12-29 02:00')-strtotime('2017-12-28 17:30'))/60/60);
8.5
I'm trying to calculate the number of hours a person has worked on a given day. To do so, I need to get the difference between two DateTime objects in Hours, Minutes, and Seconds. So far I can successfully do so like this
$timeIn = new DateTime($time['timeIn']);
$timeOut = new DateTime($time['timeOut']);
$time['hours'] = date_diff($timeIn, $timeOut) -> format("%H:%i:%s");
This seems to work fine, until I put in a test case where the employee forgot to clock out. Now, let's say that
$timeIn = '2016-09-28 14:26:17'
$timeOut = '2016-09-30 09:53:53'
In that case, the difference SHOULD be 43:27:36 (Because there is over a day in between the timeIn and timeOut). Instead, I get 19:27:36 (as if it's just truncating the days off and returning the rest). How can I add that day onto the hours instead of truncating it? (I'm looking to get 43:27:36, NOT 1day, 19 hours, ect ect. So I'm trying to get the answer in HH:MM:SS)
As Scott suggested but with a tweak, we'll need to format this ourselves, but we have nifty sprintf to help:
$start = new \DateTime("2016-09-28 14:26:17");
$end = new \DateTime("2016-09-30 09:53:53");
$interval = $end->diff($start);
$time = sprintf(
'%d:%02d:%02d',
($interval->d * 24) + $interval->h,
$interval->i,
$interval->s
);
I prefer using the \DateTime objects - although there shouldn't be much of a difference in the end date_diff is just an alias of \DateTime::diff.
You want to check the number of days in the DateInterval object;
$start = new \DateTime("2016-09-28 14:26:17");
$end = new \DateTime("2016-09-30 09:53:53");
$interval = $end->diff($start);
$days = $interval->d;
if ($days > 0) {
echo $interval->format("%a %h:%i:%s\n");
} else {
echo $interval->format("%h:%i:%s\n");
}
date_diff() returns a DateInterval instance. Look at DateInterval::format method or DateInterval::days field. Hour, minute and second values refer to the remainder in the last day (if more than one). You are looking for a the total number of days.
$timeIn = new DateTime($time['timeIn']);
$timeOut = new DateTime($time['timeOut']);
$diff = date_diff($timeIn, $timeOut) -> format("%H:%i:%s");
$days = $diff->days;
$time['hours'] = // calculate based on days + remainder...
i use ths method to find the difference between two timestamp and get the number of seconds between those two times, and i refresh the information with jquery like a counter.
$diff = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
$time = intval(date('s', $diff));
echo $time;
When the difference is more than 60 seconds, the $time comes back to 0, like a reset.
i would like to display 1 min XX s for example
The s flag for date() will never return a value greater than 59 as it only represents the current number of seconds of a given time which can never be more than 59 before rolling over into a new minute.
If you want the total number of seconds you can actually remove your second line of code as the difference between two Unix Timestamps is always in seconds:
$time = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
echo $time;
If you want to display this as minutes and seconds you can use DateTime() which offers better tools for this:
$now = new DateTime();
$then = new DateTime('2014-06-25 14:50:03');
$diff = $now->diff($then);
echo $diff->format('%i minutes %s seconds');
format the date
$diff = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
$time = date('i:s', $diff);
echo $time;
Pass time like 1 & now 2
function diffrencePassTimeAction($DataTime){
$im = $DataTime - strtotime("now");
return $im;
}
Future time like 2 & now 1
function diffrenceFuturTimeAction($DataTime){
$im = strtotime("now") - $DataTime;
return $im;
}
this function delete (-less)
function diffrencePassTimeAction($DataTime){
if ($DataTime > 0)
return $DataTime - strtotime("now");
else
return strtotime("now"); // OR return 0;
}
Hello I try to take the difference between two dates and display it.
My problem is that the time difference I get is not the correct one.
This is my code:
$time1 = strtotime('2014-03-28 15:20:00');
$time2 = strtotime('2014-03-28 15:15:00');
$diffTime = $time1 - $time2;
echo date('H:i', $diffTime);
The result I get is:
02:05
The currect time should be this:
00:05
My guess that the date somehow takes timezone or something like this but Im not sure.
Thanks.
/****************************************
$start_date = new DateTime('23:58:40'); *These two still give
$end_date = new DateTime('00:00:00'); *a wrong answer
*****************************************/
$start_date = new DateTime('23:58:40');
$end_date = new DateTime('00:11:36');
$dd = date_diff($end_date, $start_date);
//Giving a wrong answer: Hours = 23, Minutes = 47, Seconds = 4
echo "Hours = $dd->h, Minutes = $dd->i, Seconds = $dd->s";
So what you're actually doing here is generating two UNIX timestamps (numbers) and then subtracting them. then you're passing the resulting number as if it were still a timestamp to date().
essentially $diffTime is the number of seconds between your two times. you could divide by 60 to get minutes, and so on and so forth, but PHPs DateTime objects are much better.
From the PHP docs:
http://pl1.php.net/strtotime
Note:
Using this function for mathematical operations is not advisable. It is better to use DateTime::add() and DateTime::sub() in PHP 5.3 and later, or DateTime::modify() in PHP 5.2.
try this
<?php
$time1 = strtotime('2014-03-28 15:20:00');
$time2 = strtotime('2014-03-28 15:15:00');
echo round(abs($time1 - $time2) / 60,2). " minute"
?>
Below is the solution of date time in years,days.hours,minutes and seconds.
$time1 = strtotime('2014-03-28 15:20:00');
$time2 = strtotime('2014-03-28 15:15:00');
$diffTime = $time1 - $time2;
$y = ($diffTime/(60*60*24*365));
$d = ($diffTime/(60*60*24))%365;
$h = ($diffTime/(60*60))%24;
$m = ($diffTime/60)%60;
$s = ($diffTime)%60;
echo "Minutes - " .$m;
echo "<br/>";
I have two variables stored in my database containing the following data:
$date_published = 2012-05-04 00:00:00; //Straight from DB datetime field
$advert_duration = 15;
I want to show an advert for 15 days from the date it was published. To do so I need to work out the time difference.
I have read various places online about calculating time difference and have come up with the below code
In my attempt to work out the equation I can't seem to calculate the differences between $now - the date today, the $date_published and the $advert_duration. I can't get the correct result:
function days_left($date_published, $advert_duration){
$date = new DateTime($date_published);
$now = new DateTime();
$days_elapsed = $date->diff($now)->format("%d");
$days_left = $advert_duration - $days_elapsed;
return $days_left;
}
function getDaysLeft( $date, $duration )
{
// create $date and modify it by $duration
$date = new DateTime( $date );
$date->modify( sprintf( '+%d days', $duration ) );
// calculate the difference
$now = new DateTime();
$daysElapsed = (int) $now->diff( $date )->format( '%a' );
// set to negative value, if modified $date is before $now
if( $date < $now )
{
$daysElapsed = $daysElapsed * -1;
}
return $daysElapsed;
}
var_dump(
getDaysLeft( '2012-05-04 00:00:00', 15 ),
getDaysLeft( '2012-07-04 00:00:00', 15 )
);
If you're fetching your ad from the database, you can simply use a date function to calculate this :
WHERE DATE_SUB(CURDATE(),INTERVAL 15 DAY) >= date
Or you can do this in PHP (you'll get an UNIX timestamp) :
$date = strtotime('+15 days', strtotime($date_published));