I am using gmdate in PHP to convert seconds to H:i:s
I have this code:
echo gmdate("H:i:s", '480002');
So i should be converting 480002 seconds to H:i:s which should show
133:20:02
but its only showing
13:20:02
Use this
<?php
$init = 480002;
$hours = floor($init / 3600);
$minutes = floor(($init / 60) % 60);
$seconds = $init % 60;
echo "$hours:$minutes:$seconds";
?>
gmdate is working with date values not dateinterval values, H in the format string therefore represents the number of hours since midnight. Generally days don't have 133 hours, so this is interpreted as 5 days and 13 hours. You haven't asked it to display the days part, so it just shows 13 hours.
There are various ways to calculate the total number of hours.
I'd suggest looking at this question:
Calculate number of hours between 2 dates in PHP
Related
I have two times and I want to subtract them by using a PHP built-in function or Carbon. For example, I have following times:
00:05:10, i.e. 5 Minutes 10 Seconds
00:03:10, i.e. 3 Minutes 10 Seconds
If I subtract them, the total time would be 00:08:20. Can someone kindly guide me how I can make such a subtraction?
Convert to timestamp i think, after compare and substract
If you convert both times to unix timestamps, take away a "base timestamp" (For 00:00:00) from each, and then add them together you will get the number of seconds value for the 2 timestamps. Through some simple operations we can then get the total number of hours, minutes, and remaining seconds, then format them in your input style.
function add_times($a, $b) {
$base = strtotime('00:00:00');
$seconds = (strtotime($a) - $base) + (strtotime($b) - $base);
$hours = floor($seconds / 3600);
$minutes = floor($seconds / 60) % 60;
$seconds = $seconds % 60;
return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}
echo add_times('00:05:10', '00:03:10');
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 am completely stuck here. I am trying to get how many days hours and minutes to echo from a calculation from the current time to 7 days from now at 6pm. I look at the amount of seconds produced from my $difference variable and when I do the math to convert it to days hours and minutes it is correct but for some reason when I call the specific days, hours, and minutes in my output statement it is incorrect. What am I doing wrong. Here is the code.
<?php
date_default_timezone_set('America/New_York');
$nextWeek = strtotime('+7 days');
$m = date('n', $nextWeek);
$d = date('j', $nextWeek);
$y = date('Y', $nextWeek);
$difference = mktime(18,0,0,$m,$d,$y) - time();
echo '<p>Current date and time is' .date(' l F d, Y '). 'at '.date('g:i a').' You have an appointment in a week on '.date(' n/j/Y ', $nextWeek).' at 6pm. There are ' .date(' j ', $difference).' days, ' .date(' g ', $difference).' hours, and ' .date(' i ', $difference).' minutes until your appointment.</p>';
echo mktime(18,0,0,$m,$d,$y),"\n";
echo $difference;
?>
The problem is that you're using PHP's date() function on a number that doesn't represent a date. Your variable $difference represents the difference between the two dates, in seconds. To get the right output, you should write your own function to convert these seconds to number of days, hours, minutes, etc.
It might look something like this:
function getTimeText($seconds)
{
$return = array();
$return["days"] = floor($seconds/86400); // 86400 seconds in a day
$seconds -= ($return["days"]*86400);
$return["hours"] = floor($seconds/3600); // 3600 seconds in an hour
$seconds -= ($return["hours"]*3600);
$return["minutes"] = floor($seconds/60); // 60 seconds in a minute
return $return;
}
Try checking out this. The example part way down the page shows you how to find the difference between two dates in days. You should be able to use it to return the difference between the current time and the time in 7 days at 18:00 by changing the format.
I would like to show any give time in hours only.
Example:
Unix timestamp: 169200
Which is equal to 1 day and 23 hours...
But how can I convert this to hours so it shows 47:00:00 (47 hours)?
Thanks
Edit: It must show minutes and seconds too ;)
A unix timestamp is a number of seconds. There are 60 seconds per minute, 60 minutes per hour. So to convert from seconds to hours, divide by 60*60 = 3600.
If you need the minutes and seconds as well:
$timestamp = 169200;
$secs = $timestamp%60;
$mins = floor($timestamp/60);
$hours = floor($mins/60);
$mins = $mins%60;
printf("%02d:%02d:%02d", $hours, $mins, $secs);
http://ideone.com/gFKv2
$hours = $timestamp / 3600;
You could use floor() or ceil() to round the hour.
I need to be able to find the number of minutes passed in the current month so far. So from midnight of the first of the month until now.
How could I do this? So for example, 1AM on the first of the month would be give me 60 minutes.
Thanks
This should work for you:
$time = time();
$minutes = ($time-strtotime(date('Y-m-00', $time)))/60;
As of now $minutes === 15477.1
$seconds = time() - strtotime('2011-01-01 00:00:00');
$minutes = $seconds / 60;
To elaborate a bit more:
This is some simple manipulation of a unix timestamp (number of seconds since Jan 1, 1970). So you take the current timestamp and subtract what the timestamp would have been on the first of the month. This gives you total seconds that have elapsed this month. If you divide by 60, you get total minutes that have elapsed this month.