days to hours using date() function - php

I have time in minutes and I want to find out how many hours it is. But with attached code, for 1600 minutes, I get 2 hours and 40 minutes. I need it in format 26:40:00. Thanks for help.
$my_time = 1600;
echo date("H:i:s", $my_time);

Try using a DateInterval object, which is specifically built to handle intervals of time:
$interval = new DateInterval('M1600');
echo $interval->format('%H:%i:%s');

date isn't really suited for this. Instead, try this:
echo sprintf("%s:%2s:%2s",floor($my_time/3600),floor($my_time/60)%60,$my_time%60);
(This is assuming you have $my_time in seconds, not minutes. Multiply by 60 up front to get the time in seconds)

Related

PHP DateTime diff returning incorrect value in minutes

I have this PHP code which calculates the time between 2 timestamps and displays it in minutes...
$timestamp1 = new DateTime();
$timestamp1->setTimestamp('1540718680');
$timestamp2 = new DateTime();
$timestamp2->setTimestamp('1540747360');
$since_start = $timestamp1->diff($timestamp2);
echo $since_start->i.' minutes<br>';
For some reason it is returning 58 minutes insead of 477 minutes.
Where am I going wrong?
%i gives only the minutes, it's like asking for the 10s digit in a 3 digit number. You also need the hours since minutes is just the remainder after the larger units are subtracted.
You could also do the computation yourself, b-a/60, to get the number of minutes between the two specified timestamps.

Display Database Entities within a specific time frame

I want to display content from the database with dates up to 2hours ahead of time.
Example:
2018-11-09 20:00:00.000000
2018-11-08 19:00:00.000000
2018-11-06 19:00:00.000000
2018-11-06 18:00:00.000000
Lets say the time and date is
Nov 6th at 6pm. I want the bottom two entries to be displayed and the two future dates to not show until the current time is within 2hours of that time.
My code is as follows:
$cT = strtotime($row3['MissionTime']) - strtotime("now");
if($cT <= strtotime('-2 hours')) {
echo $row3['MissionTime']."<br>";
}
I've tried several different ways but I can't seem to get this to work right. Help and tips?
The reason your code doesn't work is that strtotime returns a number of seconds since the unix epoch. When you subtract two results of strtotime you will get a number of seconds difference which is as you expect. However you cannot compare that value to strtotime('-2 hours') as the output of that will be the timestamp for 2 hours before now (which right now is 1541539906), so the test will always pass. You should just compare it to 7200 instead (I'm pretty sure based on your question description that +7200 is more appropriate than -7200). so change
if($cT <= strtotime('-2 hours')) {
to
if($cT <= 7200) {
Note that it is almost certainly better to do this in your query. Try adding a condition on your time column as something like
WHERE MissionTime <= NOW() + INTERVAL 2 HOUR
And then you won't need to check in the PHP at all.
strtotime() returns a timestamp in seconds. Subtracting two timestamps gives you a difference between those two timestamps, in seconds.
So if strtotime($row3['MissionTime']) is a timestamp that's 1.5 hours in the future, and you subtract strtotime("now") from it, you end up with a difference of 5400 seconds (60 seconds * 60 minutes * 1.5 hours).
strtotime('-2 hours') gives you the timestamp for 2 hours ago, which is currently somewhere around 1.5 billion. This is not very useful for your situation.
Here are two ways to modify your code:
$cT = strtotime($row3['MissionTime']) - strtotime("now");
if($cT <= 7200) {
echo $row3['MissionTime']."<br>";
}
If the difference between $row['MissionTime'] and now is less than 7200 seconds (60 seconds * 60 minutes * two hours), $row3['MissionTime'] is either in the past or it's within the next two hours.
Alternatively:
if(strtotime($row3['MissionTime']) <= strtotime('+2 hours')) {
echo $row3['MissionTime']."<br>";
}
Basically the same, but perhaps more readable if you're not planning to use $cT for anything else. This simply checks if $row3['MissionTime'] is earlier than whatever time it will be in +2 hours.

How to use Carbon PHP to get "hours ago" for a millisecond epoch timestamp?

I have tried this, where $time = 1409065068000.
$ago = Carbon::createFromTimeStamp($time)->diffInHours();
That time stamp is from somewhere in August 26th. But my code returns me:
391015508
It shouldn't be like this. The hours ago should rather be 48 or something.
diff implies you're trying to get a different between two values, but the only value you've supplied is your timestamp. So Carbon's probably going against your time v.s. the epoch:
1409065068000 / 1000 = 1409065068 seconds
1409065068 / 60 / 60 = 391406963 hours
If you'd read the docs: https://github.com/briannesbitt/Carbon#api-difference you'd see that the various diff functions take in another carbon object that you want to diff against.
Carbon::createFromTimestamp takes epoch timestamp in seconds as input. If you want to input epoch timestamp in milliseconds then you can use Carbon::createFromTimestampMs
$ago = Carbon::createFromTimeStampMs($time)->diffInHours();
This will return the correct value.
Following code may solve your problem-
$time = $dt->timestamp(1409065068000)->timezone('Europe/London');
echo Carbon::now()->diffForHumans(Carbon::now()->subYear($time));
Try for this code, hope it will work.
However, it is highly recommended to take a look following tutorials-
https://github.com/briannesbitt/Carbon
http://tisuchi.com/php-date-time-customization-carbon/

Countdown with hours and minutes - compute the difference

I have two dates:
$today = '2012-12-01 10:40:00';
$check = '2012-12-03 12:00:00';
How can I show countdown for this dates?
Should show me:
Count: 49 hours and 20 minutes. I can check only hours or only minutes with function mktime, but how can i compare this?
try using DateTime::diff
<?php
$datetime1 = new DateTime('2012-12-01 10:40:00');
$datetime2 = new DateTime('2012-12-03 12:00:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%1 day %h hours %i minutes');
?>
You can change these dates to a unix timestamp with strtotime.
Then you can calculate the difference between them in seconds.
60 seconds in a minute, 60 minutes in an hour.
Assuming you want the clock to keep ticking as the user stays on the page, you don't really want to do that using PHP (unless you want to dispatch AJAX calls to the server every second to update the clock, which would suck). Do it client-side, using javascript.
Here are 25 pretty scripts that do that using jQuery: http://www.tripwiremagazine.com/2012/11/jquery-countdown-scripts.html

php - Time remaining until specific time from current time of page load

I have to admit that having not even tried to code this myself this question may be a annoying to some but I'm very surprised that I wasn't able to find a good example on the web of what I'm trying to do. Perhaps I'm just not using the right key words in my searches.
I am trying to calculate the time remaining from now (the time the page loads) until a specific date and time (let's say Wednesday May 11, 2011 12:00PM for example) and display it. I had thought it would be quite easy to find a snippet to accomplish this but no luck so far. Does anyone have some example code they have used to accomplish this before? I don't expect anyone to write this for me from scratch but if I can at least get pointed in the right direction it would be extremely helpful.
I'd use the DateInterval and DateTime functions:
$now = new DateTime();
$future_date = new DateTime('2011-05-11 12:00:00');
$interval = $future_date->diff($now);
echo $interval->format("%a days, %h hours, %i minutes, %s seconds");
You'll need a version of PHP that's at least 5.3 to do it this way - otherwise, do what helloandre recommends.
I think it will usefull
$startdate="2008-06-22 20:38:25";
$enddate="2008-06-29 21:38:49";
$diff=strtotime($enddate)-strtotime($startdate);
echo "diff in seconds: $diff<br/>\n<br/>\n";
// immediately convert to days
$temp=$diff/86400; // 60 sec/min*60 min/hr*24 hr/day=86400 sec/day
// days
$days=floor($temp); echo "days: $days<br/>\n"; $temp=24*($temp-$days);
// hours
$hours=floor($temp); echo "hours: $hours<br/>\n"; $temp=60*($temp-$hours);
// minutes
$minutes=floor($temp); echo "minutes: $minutes<br/>\n"; $temp=60*($temp-$minutes);
// seconds
$seconds=floor($temp); echo "seconds: $seconds<br/>\n<br/>\n";
echo "Result: {$days}d {$hours}h {$minutes}m {$seconds}s<br/>\n";
echo "Expected: 7d 0h 0m 0s<br/>\n";
echo "time isss".time();
echo $date = date('Y-m-d H:i:s')";
?>
first you'll need to calculate the difference in seconds using time() and strtotime(). Then you can translate those seconds into days/hours/minutes/seconds.

Categories