I have a date like 3/3/2012 10:56:34 and i'd like to convert it to a countdown (like ebay).
It doesn't have to be dynamic, just something like
3h 2m
2d 4h
3m
etc
It doesn't have to show years, just days, hours, mins, secs, whichever is applicable.
So if there's over a day to go, it'll show days and hours, if under a day, just hours mins, if under an hour, just mins.
Is there a simple way to do this?
UPDATE
This is what I have, but doesn't work (fixed)
$timediff = round(strtotime($rs[ends]) - strtotime($now));
while ($timediff > 86400) { $timediff = $timediff - 86400; $days++; }
while ($timediff > 3600) { $timediff = $timediff - 3600; $hours++; }
while ($timediff > 60) { $timediff = $timediff - 60; $mins++; }
$secs = $timediff;
echo $days . "d " . $hours . "h " .$mins . "m";
Use strtotime to convert your string to the Unix timestamp, then calculate the difference between now and the timestamp and then you'll easily manage to extract days, hours, minutes, etc...
Example for secs and mins from the top of my head (applicable for $timediff < 1 hour, in your case):
$seconds = $timediff % 60; $minutes = floor($timediff / 60);
etc, etc...
first use strtotime to convert to the unix time stamp if necessary.
simply calculate the times you want it to count down by:
1day is 86400 seconds.
1hour is 3600 seconds
Use SQL to get the time stamp then simply if and ifelse statements.
$currenttime=time();
if($currenttime-$somearray[0][time_stamp]<3600)
{
$seconds=$curenttime-$somearray;
echo"$seconds seconds ago";
}
elseif($currenttime-$somearray[0][time_stamp}<86400)
{
$seconds=$currenttime-$somearray;
$minutes=floor($seconds/60);
echo"$minutes minutes ago";
}
and just continue the elseif statements for whatever intervals you would like.
Related
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";
I'm trying to create a button or link that will expire after 1 hour.
I'm setting the time the visitor hit the page with a cookie.
Most of the code examples I have seen only give the time that has passed and not the time left.
example: Link will expire in 0 hours, 30, mins and 34 seconds
This is just some rough code :
//Setting cookie example
setcookie('previous_time', time(), time()+3600*1);
$current_time = time();
$previous_time = $_COOKIE['previous_time'];
$time_diff = $current_time-$previous_time;
This is where I'm stuck, I have no idea how to convert the $time_diff timestamp
into a format like "expire in 0 hours, 30, mins and 34 seconds"
Many thanks.
To format your time difference, just do some math, since your $time_diff is just the number of seconds between the two times:
$hours = floor( $time_diff / 3600);
$minutes = floor( ($time_diff / 60) % 60);
$seconds = $time_diff % 60;
echo "$hours hours, $minutes minutes, $seconds seconds\n";
So, a value of 20712 would produce :
5 hours, 45 minutes, 12 seconds
Using your formula comparing timestamps, the difference is in seconds.
So, $time_diff / 60 gets you minutes; divide by another 60 to get hours; etc.
I agree with nickb that cookie based is tamper-able, but saying you mark the first visit somehow for an hour ahead when the link will expire:
// set when we are counting down to
setcookie('expires_at', time()+3600, time()+3600);
// we are counting down not up (for "expires in" not "valid since" logic)
$time_diff = $_COOKIE['expires_at'] - time();
$minutes = floor($time_diff / 60);
$seconds = floor($time_diff % 60);
// zero hours since the link will only be valid for one hour max
echo sprintf('expire in 0 hours, %d mins and %d seconds', $minutes, $seconds);
Then you can do:
if($time_diff > 0){
echo '...';
}
For example if I have:
$seconds = 3744000; // i want to output: 43 days, 8 hours, 0 minutes
Do I have to create a function to convert this? Or does PHP already have something built in to do this like date()?
function secondsToWords($seconds)
{
$ret = "";
/*** get the days ***/
$days = intval(intval($seconds) / (3600*24));
if($days> 0)
{
$ret .= "$days days ";
}
/*** get the hours ***/
$hours = (intval($seconds) / 3600) % 24;
if($hours > 0)
{
$ret .= "$hours hours ";
}
/*** get the minutes ***/
$minutes = (intval($seconds) / 60) % 60;
if($minutes > 0)
{
$ret .= "$minutes minutes ";
}
/*** get the seconds ***/
$seconds = intval($seconds) % 60;
if ($seconds > 0) {
$ret .= "$seconds seconds";
}
return $ret;
}
print secondsToWords(3744000);
This is very simple and easy to find days , hours, minute and second in core php :
$dbDate = strtotime("".$yourdbtime."");
$endDate = time();
$diff = $endDate - $dbDate;
$days = floor($diff/86400);
$hours = floor(($diff-$days*86400)/(60 * 60));
$min = floor(($diff-($days*86400+$hours*3600))/60);
$second = $diff - ($days*86400+$hours*3600+$min*60);
if($days > 0) echo $days." Days ago";
elseif($hours > 0) echo $hours." Hours ago";
elseif($min > 0) echo $min." Minutes ago";
else echo "Just now";
An easy way to accomplish this nowadays is using DateTimeImmutable, DateInterval and PHP 5.5.0 or higher:
$seconds = 3744000;
$interval = new DateInterval("PT{$seconds}S");
$now = new DateTimeImmutable('now', new DateTimeZone('utc'));
$difference = $now->diff($now->add($interval))->format('%a days, %h hours, %i minutes');
The result will be:
43 days, 8 hours, 0 minutes
The code adds the seconds to a date and calculates the difference to it. Like this, the seconds are transformed into the specified days, hours and minutes.
Warning 1: Working without UTC - Clock changes
You may not specify the DateTimeZone in the constructor of the DateTimeImmutable object to UTC.
$now = new DateTimeImmutable();
There are regions in this world, where the clock changes on specific days of the year. Most countries in the EU change between a summer- and winter-time for example.
If your date interval overlaps the day on that a clock change occurs and your server is set to the related region for that clock change, the result might change as well. This is best shown with the following example:
$twentyFourHours = new DateInterval('PT24H');
$twentyFiveHours = new DateInterval('PT25H');
//Pacific time changed from summer- to winter-time on that day
$summerToWinter = new DateTimeImmutable('2018-11-04');
If you add 24 hours to the $summerToWinter date, you will get the following result:
$extra24Hours = $summerToWinter->add($twentyFourHours);
echo $summerToWinter->format('y-m-d H:i');
echo $extra24Hours->format('y-m-d H:i');
echo $summerToWinter->diff($extra24Hours)->format('%a days, %h hours, %i minutes');
18-11-04 00:00
18-11-04 23:00
0 days, 24 hours, 0 minutes
As you can see, between 00:00 and 23:00 on that day lay 24 hours, which is technically correct. Because of the clock change the timelap between 02:00 and 03:00 occured twice on that day.
Adding 25 hours will result in this:
$extra25Hours = $summerToWinter->add($twentyFiveHours);
echo $summerToWinter->format('y-m-d H:i');
echo $extra25Hours->format('y-m-d H:i');
echo $summerToWinter->diff($extra25Hours)->format('%a days, %h hours, %i minutes');
18-11-04 00:00
18-11-05 00:00
1 days, 0 hours, 0 minutes
As we can see, 1 day elapsed, that has had 25 hours. If this is applied for the 3744000 seconds from the original question, the result would show:
43 days, 7 hours, 0 minutes
The information, that an elapsed day has had 25 hours, is not shown though.
Also, I was not able to recreate the same effect for a day that changes the clock from winter to summer time, that should only elapse 23 hours.
Warning 2: Working with the raw DateInterval object
Using this code without DateTimeImmutable will cause the wrong output:
$seconds = 3744000;
$interval = new DateInterval("PT{$seconds}S");
$difference = $interval->format('%a days, %h hours, %i minutes, %s seconds');
Now, only the seconds are set in the DateInterval object. $difference would be:
(unknown) days, 0 hours, 0 minutes, 3744000 seconds
I like Ian Gregory's answer the most and upvoted it but thought i'd just simplify it a little bit :
function secondsToWords($seconds)
{
$days = intval(intval($seconds) / (3600*24));
$hours = (intval($seconds) / 3600) % 24;
$minutes = (intval($seconds) / 60) % 60;
$seconds = intval($seconds) % 60;
$days = $days ? $days . ' days' : '';
$hours = $hours ? $hours . ' hours' : '';
$minutes = $minutes ? $minutes . ' minutes' : '';
$seconds = $seconds ? $seconds . ' seconds' : '';
return $days . $hours . $minutes . $seconds;
}
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.
What is the best way to figure out if timestamp 1263751023 was more than 60 min ago?
$time = 1263751023;
if((time() - $time) > 60 * 60)
{
echo "Yes";
}
There are two basic way to figure this out. You can either figure out what an hour ago was and then check to see if the time you are checking was after that.
(time() - (60*60)) > $time;
The other way is you check what an hour after the time you are checking was, and see if that has passed yet.
($time + (60*60)) < time();
Oh, and the last is to check the difference between the two times, which will get you the number of seconds that have passed
(time() - $time) > (60*60)
All will get you the same answer.
One way is to calculate the difference between the one timestamp and the current timestamp:
$diff = time() - $timestamp;
And then test if that value is greater than 3600 (60 minutes with each 60 seconds):
$timestamp = 1263751023;
$diff = time() - $timestamp;
if ($diff > 3600) {
// timestamp is more than 60 minutes ago
}
$hour = 60*60; // one hour
$time = 1263751023; // zhere you could also use time() for now
if ($time + $hour < time())
{
// one hour a go
}