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 '...';
}
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 need to convert number of seconds to date, and then show time difference as days, hours and seconds.
But for some reason after some number of days, in last hour before next full day I get negative hours value. Right now this problem occures if date is greater then 38 days (it's before 1st November). Maybe tomorrow this value will be different, I'm not sure.
Code:
$s = 84600; // 23.5 h in seconds
$s += (60*60*24)*38; // add 38 days in seconds
$d = (new \DateTime())->modify("-".$s."seconds");
echo (new \DateTime())->diff($d)->format("%a days %h:%I");
// shows: 38 days -1:30
Same code with 1 day of difference:
$s = 84600; // 23.5 h in seconds
$s += (60*60*24)*37; // add 37 days in seconds
$d = (new \DateTime())->modify("-".$s."seconds");
echo (new \DateTime())->diff($d)->format("%a days %h:%I");
// shows: 37 days 23:30
PHP version 5.6.2. Tested on localhost, server and http://sandbox.onlinephpfunctions.com/ with same result.
Not sure if you looking for this kind of work around
<?php
$s= 84600;
$ts= strtotime('38 day 30 second', 0);
$difference=$ts-$s;
$days = floor($difference / 86400);
$hours = floor(($difference - $days * 86400) / 3600);
$minutes = floor(($difference - $days * 86400 - $hours * 3600) / 60);
$seconds = floor($difference - $days * 86400 - $hours * 3600 - $minutes * 60);
echo "{$days} days {$hours} hours {$minutes} minutes {$seconds} seconds";
?>
//Output 37 days 0 hours 30 minutes 30 seconds
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 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.
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.