I currently made a function which tells from timestamp the hours, minutes and seconds left. But when the item is ending in more than 24 hours it should show example 48 hours and not resetting it. How can i do this?
I have days already, but i should be only in hours, minutes and seconds, ex.
76 hours, 20 minutes & 20 seconds.
$time = strtotime($row['start_date']) - time();
$days = floor($time / (60 * 60 * 24));
$time -= $days * (60 * 60 * 24);
$hours = floor($time / (60 * 60));
$time -= $hours * (60 * 60);
$minutes = floor($time / 60);
$time -= $minutes * 60;
$seconds = floor($time);
$time -= $seconds;
$hours = ($hours<10) ? "0" . $hours : $hours;
$minutes = ($minutes<10) ? "0" . $minutes : $minutes;
$seconds = ($seconds<10) ? "0" . $seconds : $seconds;
$days = floor($time / (60 * 60 * 24));
$time -= $days * (60 * 60 * 24);
Your code removes the day part from the timestamp, that's why.
Related
If I add 1 hour and 35 minutes in PHP and subtract 4 hours, I get a remaining time of -3 hours and 30 minutes.
But correct would be -2 hours and 30 minutes. Where’s the mistake?
$minutes = null;
while ($z = $stmt->fetch()) {
$minutes += $z['minutes'];
}
if ($stmt = $pdo->prepare($sql)) {
$stmt->execute(array(
':user' => $user
));
$negativeTime = null;
while ($z = $stmt->fetch()) {
$negativeTime += $z['minutes'];
}
}
$minutes = $minutes - $negativeTime;
echo (floor($minutes / 60)) . " hours and " . ($minutes - floor($minutes / 60) * 60) . " minutes";
$minutes - floor($minutes/60) is positive. So what you're getting is -3 hours plus 30 minutes, which is -2:30.
You need to treat positive and negative values of $minutes differently, so that both the hours and minutes are the distance from zero. Use the abs() function to convert the time to positive, perform the calculation, then show the hours as negative if the original value of $minutes was negative.
$sign = $minutes < 0 ? '-' : '';
$minutes = abs($minutes);
$hours = floor($minutes / 60);
$minutes = $minutes % 60;
echo "$sign$hours hours and $minutes minutes";
DEMO
I'm storing time entries to a variable called duration. So right now If I log a time entry, it will be in the standard format: ex) 12:30:00
What I want to do is remove the seconds part of the time entry and round the minutes to every 15th minute. Also I'de like to remove the 0 in the front of if the times before 10.
So 09:00:00 would become 9
09:30:00 would be come 9:30
So 12:30:00 would be 12:30.
12:08:00 would be 12:15
12:34:00 would be 12:30 ect ect.
Here's the code I was using:
$duration = '';
if ($seconds < 0) {
$duration = '-';
$seconds = abs($seconds);
}
$hours = floor($seconds / 3600);
$seconds -= $hours * 3600;
$minutes = floor($seconds / 60);
$seconds -= $minutes * 60;
return $duration . sprintf('%d:%02d:%02d', $hours, $minutes, $seconds);
}
and here's a sample output:
1 => "12:20:00"
Try this.
$currentTime = strtotime('12:34:00');
echo 'Rounded Up 15 Minutes time: ' . date('H:i',round($currentTime / (15 * 60)) * (15 * 60));
//output - Rounded Up 15 Minutes time: 12:30
If this
$currentTime = strtotime('12:08:00');
echo 'Rounded Up 15 Minutes time: ' . date('H:i',round($currentTime / (15 * 60)) * (15 * 60));
//output - Rounded Up 15 Minutes time: 12:15
check demo. demo
For round, you may check with minutes zero condition like.
$currentTime = strtotime('9:00:00');
if(date('i',ceil($currentTime / (15 * 60)) * (15 * 60)) == 00){
echo date('H',ceil($currentTime / (15 * 60)) * (15 * 60));
}
I'm currently creating a CI project and i faced the folowing issue.
I want to show "posted "X" time ago" text and i found a script online but the seconds and minutes are not properly shown.
I've already searched all over the net but i couldn't find anything.
Here's my function:
$today = time();
$createdday = mysql_to_unix($ptime);
$datediff = abs($today - $createdday);
$difftext = "";
$years = floor($datediff / (365 * 60 * 60 * 24));
$months = floor(($datediff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
$days = floor(($datediff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
$hours = floor($datediff / 3600);
$minutes = floor($datediff / 60);
$seconds = floor($datediff);
Here's the full pastebin https://pastebin.com/tzBN2gZW
Any thoughts on that?
Thanks
The error is occured because you don't reduce datediff after days counting. But I think it's more suitable to use DateTime objects for such calculating
$today = time();
$createdday = mysql_to_unix($ptime);
$today_d = new DateTime();
$today_d->setTimestamp($today);
$createdday_d = new DateTime();
$createdday_d->setTimestamp($createdday);
print_r($today_d->diff($createdday_d));
demo
In a current project I have hit a wall because I need to compare two dates and find the difference between the dates (in hours), this would be easy if the server was >= 5.3, can someone help me?
I have the difference in timestamp
$diff = abs(strtotime($date1)-strtotime($date2)
but I don't know what to do next...
Thank you.
In your code $diff is the difference in seconds. You can convert the seconds to hours like this:
$hours = floor($diff / (60 * 60));
Edit: To get minutes and seconds:
$minutes = floor(($diff - $hours * 60 * 60) / 60);
$seconds = floor($diff - $hours * 60 * 60 - $minutes * 60);
Try this
echo round((strtotime($date1) - strtotime($date2))/(60*60));
In your code $diff is in seconds.
There are 3600 seconds in an hour, so:
$date1 = "2014-07-15";
$date2 = "2014-07-17";
$diff_seconds = abs(strtotime($date1)-strtotime($date2));
$diff_hours = $diff_seconds/3600;
echo $diff_seconds.' '.$diff_hours;
$time1 = "01:00";
$time2 = "04:55";
list($hours1, $minutes1) = explode(':', $time1);
$startTimestamp = mktime($hours1, $minutes1);
list($hours2, $minutes2) = explode(':', $time2);
$endTimestamp = mktime($hours2, $minutes2);
$seconds = $endTimestamp - $startTimestamp;
$minutes = ($seconds / 60) % 60;
$hours = round($seconds / (60 * 60));
echo $hours.':'.$minutes;
exit;
Outputs 4:55, should be 3:55 ?
Whats wrong here? If it is 01:00 and 02:00, it works fine, but not with the above?
Use floor instead of round...
Or just cast to integer.
$hours = (int) ($seconds / (60 * 60));
Too many calculations when PHP can do it for you with also reducing possibility of error
$time1 = Datetime::createFromFormat("h:i", "01:00");
$time2 = Datetime::createFromFormat("h:i", "04:55");
$diff = $time1->diff($time2);
var_dump($diff->format("%h %i"));
Output
string '3:55' (length=4)
You can also save yourself some time by using strtotime:
$time1 = strtotime("01:00");
$time2 = strtotime("04:55");
$seconds = $time2-$time1;
$minutes = ($seconds / 60) % 60;
$hours = floor($seconds / (60 * 60));
echo $hours.':'.$minutes;
As mentioned, using floor will produce the result you need:
Result
3:55