I have a variable $route->flighttime which echoes numbers in the following format...
0.5
1.2
1.45
How do I convert it to display HH:MM using echo?
Note: 0.5 is treated by the CMS as 50 minutes for some reason.
Based on OPs comments about the format of his initial time (where .5 is actually 50 minutes), a solution without any math
$time = floatval($route->flighttime);
echo number_format($time,2,':',null);
If the input is a floating point value of hours
First convert the floating point value to hours/minutes:
$hours = floor($route->flighttime);
$minutes = ($route->flighttime - $hours) * 60;
If the input is a hours/minutes pair tortured to fit into a float
You can either multiply by 100 instead of 60, or use this technique which might be more accurate:
list($hours, $minutes) = explode('.', sprintf('%.2F', $route->flighttime));
In any case
Then use printf (or some similar function such as sprintf) to format it properly:
printf("%02d:%02d", $hours, $minutes);
I'm assuming 0.5 means half of an hour. Use gmdate with format G:i and your time in seconds:
echo gmdate('G:i', round($x * 3600));
gmstrftime("%H:%M", $t * 60 * 60);
... where $t is float in hours. Notice use of gmstrftime instead of strftime, becouse you do not want to play with timezones.
If the current format is hour and minutes separated by a dot, you could use DateTime:
$time = DateTime::createFromFormat('G.i', '9.15');
echo $time->format('H:i');
However, in this format, 5 in the minutes section would be 5 minutes, not 50 minutes.
Related
So I need to convert an unix timestamp which is in seconds to milliseconds.
This line seems to be not working
$unixtime = strtotime($timestamp_conv."+3 hour") * 1000;
I basically need a 13 digit timestamp.
Any ideas what I'm doing wrong?
Thanks
As per the comments, $timestamp_conv already is a (second-)timestamp, you want to convert to a (milliseconds-)timestamp. You, however, also try to add some offset (3 hours) to it.
With simple arithmetics this would look like this
// add the three hours: 3 hours of 60 minutes of 60 seconds each
$timestamp_conv += 3 * 60 * 60;
// convert to milliseconds base
$unixtime = $timestamp_conv * 1000;
You can use DateTime from php, it's more intuitive.
<?php
$t = new \DateTime();
$t->setTimestamp(1492498242);
$t->modify("+3 hours");
echo $t->getTimestamp()*1000;
Hope it helps!
I have a string in the format of hh:mm:ss that I convert to an integer representing the total number of seconds. For example:
01:43:03
01 * 3600
43 * 60
03 * 1
The above example results in an integer value of 6183.
After performing some logic using this value I then need to convert the integer back to a strict format of hh:mm:ss.
Note that this isn't a time of day. It is an amount of time. Like, I spent 1 hour, 43 minutes and 3 seconds driving from one point to another.
What is the shortest way of coding this conversion so that 6183 seconds is formatted as 01:43:03 ?
Use this-
echo $time = date("h:i:s A T",'6183');
I think it will help.
You can simply use like this
<?php
$total_seconds = 160183;
$seconds = intval($total_seconds%60);
$total_minutes = intval($total_seconds/60);
$minutes = $total_minutes%60;
$hours = intval($total_minutes/60);
echo "$hours:$minutes:$seconds";
?>
Check live demo : http://sandbox.onlinephpfunctions.com/code/78772e1462879ce3a20548a3a780df5de4e16e2c
When I call strtotime("2016-05-06 15:00:00 +15.98 hours") I'd expect 2016-05-07 06:58:48 but instead I get 2016-05-10 02:00:00. What gives?
You can test here yourself:
Use strtotime: http://php.fnlist.com/date_time/strtotime
Convert output int to timestamp: http://www.epochconverter.com/
Floats aren't supported in date formatting in PHP
It seems that you can't do addition in strtotime that way. In addition, a bug for decimal point has been reported here
What you can do is add the time in 2 separate variable just like William's answer
Try this instead:
//60 * 60 * 15.98 = 57,528 seconds
$add = round(60 * 60 * 15.98);
$timestamp = strtotime("2016-05-06 15:00:00") + $add;
$dt = date("Y-m-d H:i:s", $timestamp);
echo $dt; //2016-05-07 06:58:48
This will calculate to 2016-05-07 06:58:48
As for why it incorrectly added the 15.98 hours is more complex. There has been a reported bug for this problem by PHP, though currently floats aren't supported in date formatting in PHP. Since you can not directly use floats in date formatting, you must substitute something like "1.5 years" with "18 months", or do arithmetic before and round it like this:
//60 * 60 * 15.98 = 57,528 seconds
$timeToAdd = round(60 * 60 * 15.98);
And then call strtotime() like in the above example
$date = strtotime("2016-05-06 15:00:00") + $timeToAdd;
I'm using date('H:i:s', (time() - $start['time'])) to displa time that passed since start of the script.
whatever the result of time() - $start['time'] - be it 0, 17 or whatever the date prints out like 02:00:00, 02:00:17 etc.
What may be the reason?
time returns an absolute numeric timestamp, say the numeric value for 2012-06-04 16:35:12. Your start time is a similar numeric, absolute timestamp. Subtracting one from the other will result in a very small number, which is, again, an absolute timestamp. Likely some time around the beginning of 1970. When you format that timestamp using date('H:i:s'), you only display the time portion of a timestamp like 1970-01-01 02:00:00.
Read about what UNIX timestamps actually represent.
The time difference that you're looking for is the result of time() - $start['time'], which is in seconds, which you can't simply format using date(). More along the lines of:
$diff = time() - $start['time'];
echo 'Difference in seconds: ' . $diff;
echo 'Difference in minutes: ' . $diff / 60;
echo 'Difference in hours: ' . $diff / 60 / 60;
seems you have a 2H offset added.
// save current TZ
$current_tz = date_default_timezone_get();
// Use universal time
date_default_timezone_set('UTC');
$t = time();
echo "T:".$t."<br/>";
sleep(2);
echo "T2: ".date('H:i:s', time() - $t );
// return correct TZ
date_default_timezone_set($current_tz);
If i try this code without the date_default_timezone_set('UTC') it gives me expected value + 1H from Summer Time (GMT+1). If you only need the diference between the 2 times you can use UTC for both and you only get the diference OR you can check the current time offset and subtract it.
Hope it helps
I have two times in PHP and I would like to determine the elapsed hours and minutes. For instance:
8:30 to 10:00 would be 1:30
A solution might be to use strtotime to convert your dates/times to timestamps :
$first_str = '8:30';
$first_ts = strtotime($first_str);
$second_str = '10:00';
$second_ts = strtotime($second_str);
And, then, do the difference :
$difference_seconds = abs($second_ts - $first_ts);
And get the result in minutes or hours :
$difference_minutes = $difference_seconds / 60;
$difference_hours = $difference_minutes / 60;
var_dump($difference_minutes, $difference_hours);
You'll get :
int 90
float 1.5
What you now have to find out is how to display that ;-)
(edit after thinking a bit more)
A possibility to display the difference might be using the date function ; something like this should do :
date_default_timezone_set('UTC');
$date = date('H:i', $difference_seconds);
var_dump($date);
And I'm getting :
string '01:30' (length=5)
Note that, on my system, I had to use date_default_timezone_set to set the timezone to UTC -- else, I was getting "02:30", instead of "01:30" -- probably because I'm in France, and FR is the locale of my system...
You can use the answer to this question to convert your times to integer values, then do the subtraction. From there you'll want to convert that result to units-hours-minutes, but that shouldn't be too hard.
Use php timestamp for the job :
echo date("H:i:s", ($end_timestamp - $start_timestamp));
$d1=date_create()->setTime(8, 30);
$d2=date_create()->setTime(10, 00);
echo $d1->diff($d2)->format("%H:%i:%s");
The above uses the new(ish) DateTime and DateInterval classes. The major advantages of these classes are that dates outside the Unix epoch are no longer a problem and daylight savings time, leap years and various other time oddities are handled.
$time1='08:30';
$time2='10:00';
list($h1,$m1) = explode(':', $time1);
list($h2,$m2) = explode(':', $time2);
$time_diff = abs(($h1*60+$m1)-($h2*60+$m2));
$time_diff = floor($time_diff/60).':'.floor($time_diff%60);
echo $time_diff;