User friendly countdown to midnight - php

I have a cron job that runs at midnight which resets all user limits for that day. I want to display something along the lines of Your limits reset in 1 hour 14 minutes to my users. Basically a countdown until midnight (server time).
At the moment I'm using this to find midnight:
strtotime('tomorrow 00:00:00');
which returns a timestamp for when midnight rolls over, but I have no idea how to display a user friendly countdown. Is there a PHP library for this or is this pretty easy without a library?

Simply this gives you left-minutes;
$x = time();
$y = strtotime('tomorrow 00:00:00');
$result = floor(($y - $x) / 60);
But you need to filter $result;
if ($result < 60) {
printf("Your limits rest in %d minutes", $result % 60);
} else if ($result >= 60) {
printf("Your limits rest in %d hours %d minutes", floor($result / 60), $result % 60);
}

Since you're looking for a rough estimate, you could leave out the seconds.
$seconds = strtotime('tomorrow 00:00:00') - now();
$hours = $seconds % 3600;
$seconds = $seconds - $hours * 3600;
$minutes = $seconds % 60;
$seconds = $seconds - $minutes *60;
echo "Your limit will reset in $hours hours, $minutes minutes, $seconds seconds.";

It is quite easy, just a little mathematics along with finding the difference in seconds between then and now.
// find the difference in seconds between then and now
$seconds = strtotime('tomorrow 00:00:00') - time();
$hours = floor($seconds / 60 / 60); // calculate number of hours
$minutes = floor($seconds / 60) % 60; // and how many minutes is that?
echo "Your limits rest in $hours hours $minutes minutes";

Related

Milliseconds format php

I got this number: 116041 (is it in milliseconds).
And i want to transform to something like this minuts:seconds:miliseconds
Theoretically that number should transform to something like: 1:56:xx
And I'm trying this code:
$diff = 116041;
$date = date("i:s:u",$diff);
echo $date;
But I'm getting this output:
14:01:000000
date() takes a timestamp integer. The value you are supplying equals Friday, January 2, 1970 8:14:01 AM. Notice the 14:01? That is what you are getting using date("i:s:u",$diff);
Go to Epoch Converter and enter 116041 into the field and you can see it for yourself.
This should be pretty simple math.
// Get the minutes (60000ms per minute)
$milliseconds = 116041;
$minutes = floor(116041 / 60000);
// Find the remaining milliseconds
$milliseconds = $milliseconds % 60000;
// Continue to seconds calculation...
In this case, the date or DateTime route is more complicated. Just do some simple math...
$s = 116041 / 1000;
printf("%d:%02.3f", intdiv($s, 60), fmod($s, 60));
You can try this method. please let me know if it of any help to you.
<?php
function formatMilliseconds($milliseconds) {
$seconds = floor($milliseconds / 1000);
$minutes = floor($seconds / 60);
$hours = floor($minutes / 60);
$seconds = $seconds % 60;
$minutes = $minutes % 60;
$milliseconds = $milliseconds % 1000;
$format = '%u:%02u:%02u.%02u';
$time = sprintf($format, $hours, $minutes, $seconds, $milliseconds);
return rtrim($time, '0');
}
echo formatMilliseconds(2000202123);
?>

PHP < 5.3 => Difference in hours between two dates

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;

Wrong time calculated having more than 30min

Im having a a bit strange problem, Im having this code and on output it adds always one hour more if the second time has 30 or more minutes.
$time1 = '12:00';
$time2 = '13:30';
list($hours, $minutes) = explode(':', $time1);
$startTimestamp = mktime($hours, $minutes);
list($hours, $minutes) = explode(':', $time2);
$endTimestamp = mktime($hours, $minutes);
$seconds = $endTimestamp - $startTimestamp;
$minutes = ($seconds / 60) % 60;
$hours = round($seconds / (60 * 60));
Whats happening here?
Remember the math. Rounding up everything in interval [0.5;1) equals to 1.
round(0.5) = 1
That's why you've +1 hour in case of minutes in [30;60].
Instead of using round use intval as $seconds / (60 * 60) expression always returns a float and we need only the integer part of that result

Create Day, Hour, Minute and Second countdown in PHP

So far I've got this script that counts down the days and hours, but how can I make it also do minutes and seconds?
$remaining = strtotime($ActiveListing['ListingExpires']) - time();
$days_remaining = floor($remaining / 86400);
$hours_remaining = floor(($remaining % 86400) / 3600);
I thought of a solution with date():
$remaining = strtotime($ActiveListing['ListingExpires']) - time();
list($months, $days, $hours, $minutes, $seconds) = explode(" ",date("n j H i s",$remaining));
$months--;$days--;
echo "$months months - $days days - $hours hours - $minutes minutes - $seconds seconds left";
$remaining = strtotime($ActiveListing['ListingExpires']) - time();
$days_remaining = floor($remaining/60/60/24);
$hours_remaining = floor(($remaining-($days_remaining*60*60*24))/60/60);
$minutes_remaining = floor(($remaining-($days_remaining*60*60*24)-($hours_remaining*60*60))/60);
$seconds_remaining = floor(($remaining-($days_remaining*60*60*24)-($hours_remaining*60*60))-($minutes_remaining*60));
$edate = '2027-07-06 07:01:53';
$datestr = $edate;//Your date
$date=strtotime($datestr);//Converted to a PHP date (a second count)
//Calculate difference
$diff=$date-time();//time returns current time in seconds
$days=floor($diff/(60*60*24));//seconds/minute*minutes/hour*hours/day)
$hours=round(($diff-$days*60*60*24)/(60*60));

How many seconds from mysql time format to now

I use this function to set a time date("Y-m-d H:i:s", strtotime("+2 minutes"). Now I want to compare that value with the current time to find the amount of seconds it's left.
For example compare: $next_action = 2011-01-16 18:03:00 and $now = 2011-01-16 18:01:23. To find the amount of seconds.
strtotime can convert mysql timestamps to unix timestamps. so you just convert both of them to UNIX timestamps and subtract one from other, and you'll get the difference in seconds.
$next_action = "2011-01-16 18:03:00";
$now = "2011-01-16 18:01:23";
echo strtotime($next_action)-strtotime($now);
Why did you convert them to "Y-m-d H:i:s" in the first place? Unix timestamps are much easier to work with.
$start_time = date("Y-m-d H:i:s", strtotime("+2 minutes"));
$time_diff = (time() - strtotime($start_time)); // difference in seconds
$seconds = $time_diff % 60;
$minutes = ($time_diff - $seconds) % (60 * 60);
$hours = ($time_diff - ($minutes * 60) - $seconds) / (24 * 60 * 60);
Untested, but it would probably go something like this.

Categories