How to make unix timestamp to show time over 24 hours? - php

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.

Related

Carbon show time different days hours minutes second

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);

if date time difference is over cetain amount

$now = new DateTime();
$future_date = new DateTime($date);
$interval = $future_date->diff($now);
echo $interval->format("%d days, %h hours, %i minutes, %s seconds");
Basically I want to use the difference between the two dates now and whatever the person has booked. I need to use an if statement to check if the difference is between 24 hours / 1 day.
how do i use datetime in if statements?
like
If ($interval > 24 hours) {
allow } my problem is how do i write 24 hours in php? sounds really dumb I know.
$now_ts = $now->getTime();
$future_date_ts = $future_date->getTime();
if ($future_date_ts - $now > 60 * 60 * 24) {
// more than 24 hours before $future_date
}
If you also want the diff the other way add:
$now - $future_date_ts > 60 * 60 * 24 // more than 24 hours after $future_date
This gives you a 48 hour range around $future_date.
You could instead use the UNIX timestamp ( the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
time(); will return the current Unix timestamp and you can convert your future_date to a timestamp using strtotime();
So you can calculate the difference in seconds using:
$diff_secs = strtotime($future_date) - time();
And knowing 1 day is 24 hours which is 24*60 minutes which is 24*60*60 seconds,
$diff_days = (strtotime($future_date) - time())/(24*60*60);
Or you could use the difference in hours as your comparison :
$diff_hours = (strtotime($future_date) - time())/(60*60);
if($diff_hours > 24) {
//do something if its more than 24 hours away
}

PHP Count Down Expiry Button or Link

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 '...';
}

php convert number into minutes

Say i have a variable like $speed = 5.5, what formula would I use to convert that into minutes, so in this case it would be 5 and a half minutes.
I need it to work in this:
date("Y-m-d H:i:s", strtotime("$now - $speed mins"));
Other examples, 2.25 would convert to 2 mins 15 secs, 7:75 to 7 mins 45 secs, etc
Anyone have any ideas? Never been a maths buff.
Just do it with second.
date('Y-m-d H:i:s', strtotime(sprintf('- %d second', $speed * 60)));
If you want more precision, then
date('Y-m-d H:i:s', strtotime(sprintf('- %d second', round($speed * 60))));
You could also use PHP's own DateInterval class (requires PHP 5.3) http://www.php.net/manual/en/dateinterval.createfromdatestring.php
With sample:
$interval = DateInterval::createFromDateString('5.5 minutes');
echo $interval->format('%Y-%m-%d %H:%i:%s')
Could also a Unix Timestamp for dates and 3600 = 1 hour.
For example, the current time would be: $timestamp = gmmktime();
So if you wanted to add ".5" (30 minutes) to the current time, you would say $timestamp + 1800. ".25" would be $timestamp + 900.
$minutes = floor($speed);
$seconds = ($speed - $minutes) * 60;

PHP Minutes in month

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.

Categories