I have this code to subtract two dates and get the difference between them in minutes:
date_default_timezone_set('Asia/Istanbul');
$date1 = '2014-07-01 09:07:25';
$date1 = date_create($date1);
$current_pc_date = date('Y-m-d H:i:s');
$current_pc_date = date_create($current_pc_date);
$diff = date_diff($current_pc_date,$date1);
$minutes_diff = $diff->format("%R%i");
echo $minutes_diff;
if($minutes_diff<5 && $minutes_diff>0)
{
echo 'yes';
}
else
{
echo 'no';
}
I want to subtract the dates and get the results in minutes but here it subtract the minute parts in the two dates so if I changed the hour part of the $date it still giving the same result when subtracting from the other date for example :
2014-07-01 02:25:48
2014-07-01 02:23:48
it will result in 2 minutes
if I changed the hour part it will still giving the same result :
2014-07-01 03:25:48
2014-07-01 02:23:48
it will result in 2 minutes
I want to have the result in the second example like 62 minutes
You can try:
function timeDiff($start_date,$end_date="now"){
$time_diff = (new Datetime($start_date))->diff(new Datetime($end_date));
$time_diff_minute = $time_diff->days * 24 * 60;
$time_diff_minute += $time_diff->h * 60;
$time_diff_minute += $time_diff->i;
return $time_diff_minute;
}
timeDiff('2014-07-01 03:25:48', '2014-07-01 02:23:48'); // 62
You could just convert your times to seconds, then subtract, then convert the difference to minutes:
$t1 = '2014-07-01 03:25:48';
$t2 = '2014-07-01 02:23:48';
$minutes_diff = abs(strtotime($t1) - strtotime($t2)) / 60;
echo $minutes_diff; //62
See demo
References:
strtotime() converts the date to a timestamp (seconds since Jan 1 1970)
abs() gets the absolute value of the difference (in case $t2 is greater than $t1)
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
With vbscript i can retrieve a double value from the current date/time with the command cdbl(now).
Now = '09.05.2015 21:44:10'
cdbl(Now) = 42133,9056712963
I'm looking for a equivalent solution with php that will give me back a double value from the current date/time.
cdbl return datetime in microsoft format. before comma date - number of days since 1 January 1900. After comma time - 24 hours is 1. So if you use dates within Unix epoch (code may be not effective,i wanted make it more understandable)
function cdbl($str) {
$datetime0 = new DateTime('1970-01-01');
$datetime = new DateTime($str);
// 25560 - days from 1 Jan 1900 to 1 Jan 1970
$cdbl = $datetime->diff($datetime0)->days + 25569;
// Remove time from string - it is the sane as 00:00
$str0 = preg_replace('/((T|\s+).+)$/','', $str);
// The number of seconds since the day start
$time = strtotime($str) - strtotime($str0);
// The number of seconds wittin a day
$timefullday = strtotime("$str0 + 1 day") - strtotime($str0);
$cdbl += $time / $timefullday;
return $cdbl;
}
echo cdbl('09.05.2015 21:44:10');
result:
42133.905671296
As you can see, there is a problem of rounding. If print result of the calculation time before summing, the answer will be the same as yours. I've never done calculating tasks, so I can not tell you what to do with it. The only suggestion is convert to a string :)
Well, if you choose to work outside of Unix dates you should write some code
The following function returns the current Unix timestamp as a double value:
function cdblnow(){
// time in seconds since 1 Jan 1970 (GMT)
$timeunix = time();
// add the timezone offset
$TimeZone = "Asia/Bangkok";
$dateTimeZone = new DateTimeZone($TimeZone);
$dateTime = new DateTime("now", $dateTimeZone);
$timeOffset = $dateTimeZone->getOffset($dateTime);
$timeunix = $timeunix + $timeOffset;
// determines the days between 1 Jan 1970 and today
$days = intval($timeunix / 86400);
// second count from today
$secondsremains = $timeunix % 86400;
// 25569 days difference between microsoft and unix time stamp start
$now_date = $days + 25569;
// 0.0000115741 represents one second at the cdbl-function from microsoft
$now_time = $secondsremains * 0.0000115741;
return $now_date + $now_time;}
This function returns the unix time stamp from a double:
function GetUnixTimeFromCdblNow($cdbl){
$days = intval($cdbl);
$seconds = round($cdbl - $days,9);
$timeunix = (($days -25569) * 86400);
$timeunix = $timeunix + intval($seconds / 0.0000115740);
return $timeunix;}
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;
}
I have something like that for example: 01:06:22 this represents 1hour, 6minutes and 22seconds. I want to take that, and multiple it by 6 and add it to some other hour such as 04:23 which is 4AM and 23Minutes not 4hours and 23 minutes.
Basically, as a result I expect that:
01:06:22
* 6 = 6hours 38minutes canceling the remaining seconds which are 12 in this case
Now, I want to take that and append it to other hour, 04:23 in this case, so the result would be:
11:01.
I have no clue how to start and do it, unfortunately.
Any help is appriciated!
Clarifications
The time that I have to multiple by 6 will never exceed 2 hours.
All the times are in the same format.
With DateTime it is simple:
$time = '01:06:22';
$dateSeconds = new DateTime("1970-01-01 $time UTC");
$seconds = $dateSeconds->getTimestamp() * 6;
$interval = new DateInterval('PT'.$seconds.'S');
$date = new DateTime('1970-01-01 04:23:00 UTC');
$date->add($interval);
echo $date->format('H:i:s');
Other solution with strtotime and gmdate. (Similar to Suresh but working):
$date = strtotime('1970-01-01 01:06:22 UTC');
$add = strtotime('1970-01-01 04:23:00 UTC');
$date = (($date*6)+$add);
echo gmdate('H:i:s', $date);
This is a solution if you want to implement it yourself.
The thing about timecode is that it can become really heavy with the if the if conditions etc if you don't do it right.
The best Way I thought of to deal with this is to convert everything to second.
so 01:06:22 would become:
numberOfSecond = 22 + 06 * 60 + 01 * 60 * 60
How to get the 22, 06 etc from the String? You can use Regex.
What you will need:
a function to extract the different values (hours, minute, second)
a function to convert the timecode into second
a function to convert back into timecode
the functions to multiply, add etc...
You might want to create a class for it.
You can try like this:
$date = strtotime('01:06:22');
$add = strtotime('00:04:23');
$date = ($date*6)+$add;
echo date('H:i:s', $date);
Note: Code is not tested.
First of all you want to multiply a time span by a factor. The easiest way to do this is to convert the span to seconds and do a straight multiply:
$date =DateTime::createFromFormat('!H:i:s', '01:06:22', new DateTimeZone('UTC'));
$seconds = $date->getTimestamp();
This code works by pretending that the time is a moment during the Unix epoch start so that it can then get the number of seconds elapsed since the epoch (the timestamp). That number is equal to the duration of the time span in seconds. However, it is vitally important that the input is interpreted as UTC time and not as something in your local time zone.
An equivalent way of doing things (as long as the input is in the correct format) which is lower-tech but perhaps less prone to bugs would be
list($h, $m, $s) = explode(':', '01:06:22');
$seconds = $h * 3600 + $m * 60 + $s;
Now the multiplication:
$seconds = $seconds * 6;
If you want to only keep whole minutes from the time you can do so at this stage:
$seconds = $seconds - $seconds % 60;
The final step of adding the result to a given "time" is not clearly specified yet -- does the reference time contain date information? What happens if adding to it goes over 24 hours?
Self explanatory :
$initialTime = '01:06:22';
$timeToAdd = '04:23';
$initialTimeExploded = explode( ':' ,$initialTime );
$initialTimeInMintues = ( $initialTimeExploded[0] * 60 ) + $initialTimeExploded[1];
$initialTimeInMintuesMultipliedBySix = $initialTimeInMintues * 6;
$timeToAddExploded = explode( ':' ,$timeToAdd );
$timeToAddExplodedInMintues = ( $timeToAddExploded[0] * 60 ) + $timeToAddExploded[1];
$newTimeInMinutes = $initialTimeInMintuesMultipliedBySix + $timeToAddExplodedInMintues;
$newTime = floor( $newTimeInMinutes / 60 ) .':' .($newTimeInMinutes % 60);
echo $newTime;
Result :
10:59
I want to convert times to rounded times.
Therefore I only want to use intervals in seconds.
It works well for quarter hours and full hours. But not for days. Then it is messed up. But I can't understand why
This is my function:
function formatToMySQLTime($sTime,$iInterval){
switch ($iInterval){
case 1: // 15 minutes;
$iDivider = 15 * 60;
break;
case 2: // 1 hour
$iDivider = 60 * 60;
break;
case 3: // 1 day
$iDivider = 60 * 60 * 24;
break;
}
$iRemainder = strtotime($sTime) % $iDivider;
$iRoundTime = strtotime($sTime) - $iRemainder;
$sTime = date('Y-m-d H:i:s', $iRoundTime);
return $sTime;
}
Here is the output:
echo formatToMySQLTime('2013-10-21 03:23:00',1);
2013-10-21 03:15:00
echo formatToMySQLTime('2013-10-21 03:23:00',2);
2013-10-21 03:00:00
echo formatToMySQLTime('2013-10-21 03:23:00',3);
2013-10-21 02:00:00
The third output is wrong, it should be 2013-10-21 00:00:00. What is my mistake?
You have timezone issue, because strtotime function is not timezone aware, and date is. Instead of setting global timezone, you can set it locally with appending UTC string to the $sTime variable, and instead of using date() use gmdate():
// it doesn't matter in which timezone your are, function works with UTC
date_default_timezone_set('Europe/Berlin');
function formatToMySQLTime($sTime, $iInterval) {
switch ($iInterval){
case 1: // 15 minutes;
$iDivider = 15 * 60;
break;
case 2: // 1 hour
$iDivider = 60 * 60;
break;
case 3: // 1 day
$iDivider = 60 * 60 * 24;
break;
}
$U = strtotime($sTime . ' UTC');
$iRoundTime = $U - $U % $iDivider;
return gmdate('Y-m-d H:i:s', $iRoundTime);
}
echo formatToMySQLTime('2013-10-21 03:23:00',1);
echo formatToMySQLTime('2013-10-21 03:23:00',2);
echo formatToMySQLTime('2013-10-21 03:23:00',3);
Run code.
Update:
Instead of modifying function for every interval, you can make function a little more generic and readable, with the help of DateTime classes, like:
function formatToMySQLTime($sTime, $iInterval) {
$dt = new DateTime($sTime, new DateTimezone('UTC'));
$U = $dt->getTimestamp();
$dt2 = clone $dt;
$interval = DateInterval::createFromDateString($iInterval);
$iDivider = $dt2->add($interval)->getTimestamp() - $U;
$dt->setTimestamp($U - $U % $iDivider);
return $dt->format('Y-m-d H:i:s');
}
echo formatToMySQLTime('2013-10-21 03:23:00', '15 minute');
echo formatToMySQLTime('2013-10-21 03:23:00', '1 hour');
echo formatToMySQLTime('2013-10-21 03:23:00', '1 day');
Run code.
What is my mistake?
The whole approach is flawed.
If you check your $iRemainder, you’ll see it contains a date value like Thu, 01 Jan 1970 23:01:48 +0100 – and subtracting that from your original value and adding “a days worth of seconds” again of course gets messy with the different times caused by DST.
When working with unix timestamps, assuming a day would always consist of a certain amount of seconds is just plain wrong – because of things like DST.
Btw., you are not actually “rounding”, based on your examples – for the first two, you are rounding up to the next 15 minutes or full hour, but for the third one you want to round down to the day – what’s the logic behind that?
I’d suggest you look at the necessary parts of the date individually, and then return a formated date with the rest just set to matching value manually.