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
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!
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 have a problem with my script and I dont understand where is the problem. So I have this code :
$i_now = strtotime(date('Y-m-d'));
$i_date_last_bonus = strtotime($o_member->date_last_bonus);
$i_datediff = round(abs($i_now - $i_date_last_bonus) / 86400);
print_r("Date Now :".date('Y-m-d'));
print_r("Last Win :".$o_member->date_last_bonus);
I get the $i_datediff = 1 and I dont understand why because in the print_r I have Date Now :2015-12-04 and Last Win:2015-12-03
Can you help me please where I make the error ? Thx in advance and sorry for my english
In one day there are 24 Hrs, in each hour there are 60Min, in each min there are 60sec. Hence, there are 24*60*60 = 86400sec in one day.
Now, The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Means it returns seconds.
so, i_now = 1449187200 andi_date_last_bonus = 1449100800
Difference is 86400sec.
Now $i_datediff = round(abs($i_now - $i_date_last_bonus) / 86400); this is converting seconds in days.
And Difference is 86400 / 86400 = 1
means 1 day.
This result is correct as you get the number of seconds between two dates first and then you divide it by the number of seconds in 24 hours (86400) which gives you 1 (day) as a result.
Kindly help me with converting JSON time data to 12 hrs human readable time in PHP.
Data I got as time from JSON is: 1380, 1395 and so on
Real time corresponding to JSON time data is as follows.
For 1380 is 11.00PM
For 1395 is 11.15PM
Now kindly tell me how to convert (1380, 1395, etc) into human readable time 12hrs in PHP
Thanks in Advance.
It looks like 1380 is number of minutes for that day.
You can use gmdate(). First convert the minutes into seconds (by multiplying by 60) and then use it in gmdate function.
echo gmdate("h:i A", 1380*60);
Assuming that this value is simply the number of minutes since midnight:
$value = 1395;
$hours = floor($value / 60);
$minutes = $value - ($hours * 60);
$time = $hours . ':' . $minutes;
Though some clarification on what happens on daylight savings days would be useful
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.