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
Related
From a time like:
$time_on_seconds = 18000;
It is equal to 5 hours.
Now for subtract seconds Im using this code:
$new_time_on_seconds = date("H:i", strtotime($time_on_seconds) - 60);
It return me 23:59 when this should return me 4:59
Just remove strtotime.
Strtotime does what the name says it takes a str (string) and makes it time (seconds) so using that on a already number won't work.
$time_on_seconds = 18000;
echo $new_time_on_seconds = date("H:i", $time_on_seconds - 60);
But keep in mind date() is timezone aware so you may end up with a wrong result depending on your timezone.
Set the timezone to UTC and you should get the correct result.
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
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.
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;