I'm trying to get the total minutes and seconds that have elapsed from the following step by step:
1: strtotime('now') - strtotime('-1 day');
2: Ill get something back like 120 lets say...
3: So... 120 = 2 minutes -- My problem is the remainder though!
4: Examples:
130 = 2 minutes 10 seconds
130 / 60 = 2.1666~
121 = 2 minutes 1 seconds
121 / 60 = 2.0166~
122 = 2 minutes 2 seconds
122 / 60 = 2.0366~
5: So using the example 122, I have 2 minutes,
but how to I grab the 2 seconds from there?
I was trying to explode on the period,
and use the remainder, but that's not right.
PS: I never had a gift for Math.
You can also use DateTime math (php >= 5.3):
$d1 = new DateTime(strtotime('now'));
$d2 = new DateTime(strtotime('-1 days'));
$diff = $d1->diff($d2);
echo "{$diff->i} minutes, {$diff->s} seconds\n";
This is how you can use PHP's DateTime::diff which returns DateInterval
$dt = new DateTime();
$dt2 = new DateTime("-120 seconds");
$diff = $dt->diff( $dt2 );
echo( $diff->format("%i min and %s sec") );
Related
i try to make time difference with carbon
$dt = Carbon::parse('2018-07-15 00:00:00');
$now = Carbon::now('Asia/Dubai'); //set current time
$seconds = $now->diffInSeconds( $dt ); //difference turn into second
$days = $dt->diffInDays($dt->copy()->addSeconds($seconds));
$hours = $dt->diffInHours($dt->copy()->addSeconds($seconds)->subDays($days));
$minutes = $dt->diffInMinutes($dt->copy()->addSeconds($seconds)->subHours($hours));
$days result are 12 (its right).
$hours result are 8 (seems not right).
$minutes result are 17299 (clearly wrong).
how to get the result for example 12 day 5 hours 45 minutes
Actually functions like diffInSeconds give total difference in seconds that's why the number is so large,to get the minutes for the time difference right you can use -:
$minutes = ($now->minute - $dt->minute);
I have two Timestamps
2016-01-01 00:00:00
2016-01-02 23:59:59
Using PHP how can I calculate the number of hours and minutes between the two times and get the result as a decimal with 2 places after the .
currently I have this:
$Start = new DateTime($StartTime);
$Finish = new DateTime ($FinishTime);
$Interval = date_diff($Start,$Finish);
$Hours = $Interval->format('%h.%i');
But the result is incorrect if the user starts the timer on Day 1 and finishes on day 2.
You could multiply the number of days by 24 to convert them to hours, then sum the hours and concatenate the minutes:
$start = new DateTime('2016-01-01 00:00:00');
$end = new DateTime('2016-01-02 23:59:59');
$interval = $end->diff($start);
$days = $interval->format('%d');
$hours = 24 * $days + $interval->format('%h');
echo $hours.':'.$interval->format('%i');
You could format the DateTime as a UNIX timestamp, and then simply subtract to get the total seconds, and format the output with gmdate().
$Start = new DateTime($StartTime);
$Finish = new DateTime ($FinishTime);
$Interval = $Start->format('U') - $Finish->format('U');
$Hours = gmdate("H:i:s", $Interval);
Try this.
$Hours = $Interval->format('%a.%h.%i');
I got the following code:
$now = new DateTime();
$then = new DateTime($accountExists['sub_limit']);
$interval = $then->diff($now);
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
echo 'Diff. in minutes is: '.($hours * 60 + $minutes);
which returns the difference between 2 datetimes in minutes. If then is 2015-05-31 19:15:31 and now is 2015-05-31 19:20:31 it returns 5 minutes. But as soon as the day changes, if then changes to 2015-05-30 19:15:31 it still returns 5 minutes when it should be 1445 minutes. Could someone point out my error?
Because months, years can have an arbitrary number of minutes, you might best want to convert your dates to timestamps (seconds since epoch) so you only have to divide by 60. Fortunately, it's easy to do so:
$now = new DateTime('2015-05-31 19:20:31');
$then = new DateTime('2015-05-30 19:15:31');
$seconds = abs($now->format('U') - $then->format('U'));
$minutes = floor($seconds / 60);
print $minutes;
That is because it's a full day. So you have to calculate how many minutes one day is. So this should work for you:
Basically here I just get all days, hours, minutes and seconds from the interval and multiply them by the multiplier to get minutes out of them.
<?php
//Test data
$now = "2015-05-30 19:20:31";
$accountExists['sub_limit'] = "2015-05-30 19:15:31";
$now = new DateTime($now);
$then = new DateTime($accountExists['sub_limit']);
$interval = $then->diff($now);
$multiplier = ["days" => 60*24, "h" => 60, "i" => 1, "s" => 1/60];
$minutes = 0;
$values = array_intersect_key((array)$interval, $multiplier);
foreach($values as $k => $v)
$minutes += $v*$multiplier[$k];
echo "Diff. in minutes is: " . $minutes;
?>
output:
Diff. in minutes is: 1445
Say i have a variable like $speed = 5.5, what formula would I use to convert that into minutes, so in this case it would be 5 and a half minutes.
I need it to work in this:
date("Y-m-d H:i:s", strtotime("$now - $speed mins"));
Other examples, 2.25 would convert to 2 mins 15 secs, 7:75 to 7 mins 45 secs, etc
Anyone have any ideas? Never been a maths buff.
Just do it with second.
date('Y-m-d H:i:s', strtotime(sprintf('- %d second', $speed * 60)));
If you want more precision, then
date('Y-m-d H:i:s', strtotime(sprintf('- %d second', round($speed * 60))));
You could also use PHP's own DateInterval class (requires PHP 5.3) http://www.php.net/manual/en/dateinterval.createfromdatestring.php
With sample:
$interval = DateInterval::createFromDateString('5.5 minutes');
echo $interval->format('%Y-%m-%d %H:%i:%s')
Could also a Unix Timestamp for dates and 3600 = 1 hour.
For example, the current time would be: $timestamp = gmmktime();
So if you wanted to add ".5" (30 minutes) to the current time, you would say $timestamp + 1800. ".25" would be $timestamp + 900.
$minutes = floor($speed);
$seconds = ($speed - $minutes) * 60;
I would like to compare a difference between two timestamps and a time in base 60 or 10.
More precisely if timestamp1-timestamp2 is longer (or no) than x hours and y seconds.
I'm using DateTime and DateInterval classes, but there isn't such a function, and i don't find a clean solution.
Thanks
$time1 = new DateTime("2011-01-26 01:13:30"); // string date
$time2 = new DateTime();
$time2->setTimestamp(1327560334); // timestamps, it can be string date too.
$interval = $time2->diff($time1);
echo $interval->format("%H hours %i minutes %s seconds");
Output
11 hours 32 minutes 4 seconds
$timestamp1 = strtotime('2012-01-26 14:00:00');
$timestamp2 = strtotime('2012-01-25 17:00:00');
if (abs($timestamp1 - $timestamp2) < 60 * 60 * 5 /* (5 hours) */) {
...
Convert both the timestamp and the real time to the UNIX timestamp. Then simply subtract to get the number of seconds difference.
With datetime objects:
$interval = $TempReceiptDateTime->diff($ShipDateTime);
echo $interval->format('%R%H:%I:%s days');
more