PHP Calling just part of string item - php

So I have not yet learned PHP but our guy that would normally write the functions for our site is busy. So I'm attempting to solve this problem with very limited knowledge. I need to convert 4 digit army time (0800, 0815, 0830, 0845, 0900...) into standard time (8:00AM, 8:15AM,...). I wrote an equation but its returning all the hours as 12 and always PM. Here's my equation...
function convert_army_to_regular($time) {
$hours = substr($time, 0, 1);
$minutes = substr($time, 2, 3);
if ($hours > 12) {
$hours = $hours - 12;
$ampm = 'PM';
} else if ($hours = 12) {
$ampm = 'PM';
} else {
if ($hours < 11){
$ampm = 'AM';
}
}
return $hours . ':' . $minutes . $ampm;
}
What am I doing wrong?

You have two issues with your code, = is an assignment. You need to use == for a comparison (or === would also work, that checks the type as well).
The second issue is the substr. That function takes the position to start as parameter 1 and the number of characters to advance as 2. So your second parameter should be 2, in both examples.
$hours = substr($time, 0, 2);
$minutes = substr($time, 2, 2);
You also can cast it to an int and that will remove the leading 0 if it is less than 10.
$hours = (int)substr($time, 0, 2);
You also could do this with a regex:
echo preg_replace_callback('/(\d{2})(\d{2})/', function($match) {
$hours = (int)$match[1];
$minutes = $match[2];
$median = 'AM';
if($hours > 12 ) {
$hours = $hours - 12;
$median = 'PM';
}
return $hours . ':' . $minutes . $median;
},'1800');

Just use date to convert from military to standard time:
$armyTime = "2300";
$time_in_12_hour_format = date("g:i a", strtotime($armyTime));
echo $time_in_12_hour_format;
Result: 11:00 pm

Much easier to leverage the strtotime function and date.
$stime = '0830';
echo date("g:ia", strtotime($stime));
which results in 8:30am

Related

How to calculate negative / positive times and greater than 24:00?

I have to make a calculation with times like these that come from an array:
+04:43
03:33
-10:33
I tried using Datetime and stuff but the class just broke when it surpassed 24:00 time (10:00 + 20:00 for example). So I tried something like this:
I transformed all my hh:mm to seconds with explode:
foreach($stringSaldo as $saldo) {
$horaM[] = explode(':',$saldo);
}
$totalHora = 0;
$totalMin = 0;
foreach($horaM as $hora) {
$totalHora = ($totalHora + $hora[0]);
$totalMin =( $totalMin + $hora[1]);
}
$totalHora = $totalHora * 3600;
$totalMin = $totalMin * 60;
$totalSeconds = $totalHora + $totalMin;
Then I tried to make that seconds in time:
$hours = floor($totalSeconds / 3600);
$minutes = floor(($totalSeconds / 60) % 60);
$seconds = $totalSeconds % 60;
echo $hours. ":" . $minutes;
For some reason when I have times like: -03:34 and +01:00 the calculation fails, it gives -02:-26 but it should be -02:34.
What am I doing wrong?
I think I have this ironed out. It is important that you do the arithmetic with the absolute values and separately handle the sign. I have included some zero padding to ensure that you are getting a consistent string result. My regular expression makes the + or - optional.
Code: (Demo of your 3 test cases)
function timeToSeconds($time) {
if (!preg_match('~([+-]?)(\d{2}):(\d{2})~', $time, $out)) {
echo "failed to parse $time\n";
return 0;
} else {
return (($out[2] * 3600) + $out[3] * 60) * ($out[1] == '-' ? -1 : 1);
}
}
function secondsToTime($seconds) {
$sign = $seconds < 0 ? '-' : '';
$abs = abs($seconds);
return $sign . str_pad(floor($abs / 3600), 2, '0', STR_PAD_LEFT) . ':' . str_pad(floor($abs / 60 % 60), 2, '0', STR_PAD_LEFT);
}
$times = ['-03:34', '+01:00'];
$seconds = 0;
foreach ($times as $time) {
$seconds += timeToSeconds($time);
}
echo $seconds; // -9240
echo "\n---\n";
echo secondsToTime($seconds); // -02:34
Relevant resource: https://stackoverflow.com/a/3856312/2943403
p.s. When you split time on :, the hours may be positive or negative, but the minutes will always be positive. This is a fundamental issue with your posted snippet.

Time conversion to hours and minutes with am and pm

Currently, I have data as below. But, it wasn't a timestamp value. It was a time itself
$start_time = '150630';
$end_time = '180630';
I want to change the format like 03:06 PM – 06:06 PM i tried
$init = 150630;
$hours = floor($init / 3600);
$minutes = floor(($init / 60) % 60);
$seconds = $init % 60;
echo "$hours:$minutes:$seconds";
But the format and value is not as expected.
Please help me how to solve this
You doesn't need time converter as your string wasn't a valid timestamp. Instead, it was time itself. You just need to extract the hour, min, and second.
Refer below
<?php
$start_time = "150630";
$hours = substr($start_time, 0, 2); // this is actual hours
$mins = substr($start_time, 2, 2); // this is actual minutes
$secs = substr($start_time, 4, 2); // this is actual seconds
if ($hours < 12) {
$actTime = "$hours:$mins:$secs AM";
} else {
$actTime = ($hours-12).":$mins:$secs PM";
}
echo $actTime; // this is the time format you want
You can use str_split function as your time format is not timestamp.
It will extract hours, minutes and seconds.
check the below code :
<?php
$start_time = '150630';
$end_time = '180630';
$startTimeA = str_split($start_time, 2);
$endTimeA = str_split($end_time, 2);
$startTime = "";
$AmOrPm = "AM";
if ($startTimeA[0] > 12) {
$startTime .= (int) ($startTimeA[0] - 12);
$AmOrPm = "PM";
}
echo $startTime .= ":" . $startTimeA[1] . " " . $AmOrPm;
$endTime = "";
$AmOrPm = "AM";
if ($endTimeA[0] > 12) {
$endTime .= (int) ($endTimeA[0] - 12);
$AmOrPm = "PM";
}
echo $endTime .= ":" . $endTimeA[1] . " " . $AmOrPm;
?>
Use Php Date Function And Strtotime
strtotime — Parse about any English textual datetime description into a Unix timestamp.
And Convert It like
echo $start_time = date('h:i:a' , strtotime(150630));
//o/p 03:06:pm

Convert number of minutes into hours & minutes using PHP

I have a variable called $final_time_saving which is just a number of minutes, 250 for example.
How can I convert that number of minutes into hours and minutes using PHP in this format:
4 hours 10 minutes
<?php
function convertToHoursMins($time, $format = '%02d:%02d') {
if ($time < 1) {
return;
}
$hours = floor($time / 60);
$minutes = ($time % 60);
return sprintf($format, $hours, $minutes);
}
echo convertToHoursMins(250, '%02d hours %02d minutes'); // should output 4 hours 17 minutes
echo date('H:i', mktime(0,257));
$hours = floor($final_time_saving / 60);
$minutes = $final_time_saving % 60;
You can achieve this with DateTime extension, which will also work for number of minutes that is larger than one day (>= 1440):
$minutes = 250;
$zero = new DateTime('#0');
$offset = new DateTime('#' . $minutes * 60);
$diff = $zero->diff($offset);
echo $diff->format('%a Days, %h Hours, %i Minutes');
demo
#Martin Bean's answer is perfectly correct but in my point of view it needs some refactoring to fit what a regular user would expect from a website (web system).
I think that when minutes are below 10 a leading zero must be added.
ex: 10:01, not 10:1
I changed code to accept $time = 0 since 0:00 is better than 24:00.
One more thing - there is no case when $time is bigger than 1439 - which is 23:59 and next value is simply 0:00.
function convertToHoursMins($time, $format = '%d:%s') {
settype($time, 'integer');
if ($time < 0 || $time >= 1440) {
return;
}
$hours = floor($time/60);
$minutes = $time%60;
if ($minutes < 10) {
$minutes = '0' . $minutes;
}
return sprintf($format, $hours, $minutes);
}
$t = 250;
$h = floor($t/60) ? floor($t/60) .' hours' : '';
$m = $t%60 ? $t%60 .' minutes' : '';
echo $h && $m ? $h.' and '.$m : $h.$m;
4 hours and 10 minutes
Sorry for bringing up an old topic, but I used some code from one of these answers a lot, and today I told myself I could do it without stealing someone's code. I was surprised how easy it was. What I wanted is 510 minutes to be return as 08:30, so this is what the code does.
function tm($nm, $lZ = true){ //tm = to military (time), lZ = leading zero (if true it returns 510 as 08:30, if false 8:30
$mins = $nm % 60;
if($mins == 0) $mins = "0$mins"; //adds a zero, so it doesn't return 08:0, but 08:00
$hour = floor($nm / 60);
if($lZ){
if($hour < 10) return "0$hour:$mins";
}
return "$hour:$mins";
}
I use short variable names because I'm going to use the function a lot, and I'm lazy.
The easiest way is :
gmdate('H:i', $numberOfSeconds * 60)
Just in case you want to something like:
echo date('G \h\o\u\r\s i \m\i\n\u\t\e\s', mktime(0, 90)); //will return 1 hours 30 minutes
echo date('G \j\a\m i \m\e\n\i\t', mktime(0, 90)); //will return 1 jam 30 menit
function hour_min($minutes){// Total
if($minutes <= 0) return '00 Hours 00 Minutes';
else
return sprintf("%02d",floor($minutes / 60)).' Hours '.sprintf("%02d",str_pad(($minutes % 60), 2, "0", STR_PAD_LEFT)). " Minutes";
}
echo hour_min(250); //Function Call will return value : 04 Hours 10 Minutes
$m = 250;
$extraIntH = intval($m/60);
$extraIntHs = ($m/60); // float value
$whole = floor($extraIntHs); // return int value 1
$fraction = $extraIntHs - $whole; // Total - int = . decimal value
$extraIntHss = ($fraction*60);
$TotalHoursAndMinutesString = $extraIntH."h ".$extraIntHss."m";
Thanks to #Martin_Bean and #Mihail Velikov answers. I just took their answer snippet and added some modifications to check,
If only Hours only available and minutes value empty, then it will display only hours.
Same if only Minutes only available and hours value empty, then it will display only minutes.
If minutes = 60, then it will display as 1 hour. Same if minute = 1, the output will be 1 minute.
Changes and edits are welcomed.
Thanks. Here is the code.
function convertToHoursMins($time) {
$hours = floor($time / 60);
$minutes = ($time % 60);
if($minutes == 0){
if($hours == 1){
$output_format = '%02d hour ';
}else{
$output_format = '%02d hours ';
}
$hoursToMinutes = sprintf($output_format, $hours);
}else if($hours == 0){
if ($minutes < 10) {
$minutes = '0' . $minutes;
}
if($minutes == 1){
$output_format = ' %02d minute ';
}else{
$output_format = ' %02d minutes ';
}
$hoursToMinutes = sprintf($output_format, $minutes);
}else {
if($hours == 1){
$output_format = '%02d hour %02d minutes';
}else{
$output_format = '%02d hours %02d minutes';
}
$hoursToMinutes = sprintf($output_format, $hours, $minutes);
}
return $hoursToMinutes;
}
2022 answer using Carbon
Carbon::createFromTime(
intdiv($final_time_saving, 60),
($final_time_saving % 60),
0,
0)
->format('H:i')
check this link for better solution. Click here
How to convert hh:mm:ss to minutes
$minutes=$item['time_diff'];
$hours = sprintf('%02d',intdiv($minutes, 60)) .':'. ( sprintf('%02d',$minutes % 60));

PHP — Convert milliseconds to Hours : Minutes : Seconds.fractional

I've got a script that takes in a value in seconds (to 2 decimal points of fractional seconds):
$seconds_input = 23.75
I then convert it to milliseconds:
$milliseconds = $seconds_input * 1000; // --> 23750
And then I want to format it like so:
H:M:S.x // --> 0:0:23.75
Where 'x' is the fraction of the second (however many places after the decimal there are).
Any help? I can't seem to wrap my mind around this. I tried using gmdate() but it kept lopping off the fractional seconds.
Thanks.
Edit: Well, I was a bit hasty. Here's one way to do what you're asking:
function formatMilliseconds($milliseconds) {
$seconds = floor($milliseconds / 1000);
$minutes = floor($seconds / 60);
$hours = floor($minutes / 60);
$milliseconds = $milliseconds % 1000;
$seconds = $seconds % 60;
$minutes = $minutes % 60;
$format = '%u:%02u:%02u.%03u';
$time = sprintf($format, $hours, $minutes, $seconds, $milliseconds);
return rtrim($time, '0');
}
My take
function formatSeconds( $seconds )
{
$hours = 0;
$milliseconds = str_replace( "0.", '', $seconds - floor( $seconds ) );
if ( $seconds > 3600 )
{
$hours = floor( $seconds / 3600 );
}
$seconds = $seconds % 3600;
return str_pad( $hours, 2, '0', STR_PAD_LEFT )
. gmdate( ':i:s', $seconds )
. ($milliseconds ? ".$milliseconds" : '')
;
}
And then the test
$testData = array(
23, // Seconds, w/o millis
23.75, // Seconds, w/millis
23.75123456789, // Lots of millis
123456789.75 // Lots of seconds
);
foreach ( $testData as $seconds )
{
echo formatSeconds( $seconds ), PHP_EOL;
}
which yields
00:00:23
00:00:23.75
00:00:23.75123456789
34293:33:09.75
Mine is much less readable, so it must be better. :p
Basically the same idea as #ircmaxell's version. It does trim the trailing '0's and even will skip the last '.' separator if milliseconds are 0.
<?
function format_period($seconds_input)
{
$hours = (int)($minutes = (int)($seconds = (int)($milliseconds = (int)($seconds_input * 1000)) / 1000) / 60) / 60;
return $hours.':'.($minutes%60).':'.($seconds%60).(($milliseconds===0)?'':'.'.rtrim($milliseconds%1000, '0'));
}
echo format_period(23.75);
if you really want to do it using date function you are right, you have to deal with milliseconds externally, is only based on second-timestamps.
you could do something like this:
<?
$input = "23.75";
$seconds = floor($input);
$date = DateTime::createFromFormat('s', floor($seconds));
$ms = ($input-$seconds);
if($ms == 0) {
$ms = "";
} else {
$ms = ltrim($ms,"0,");
}
echo $date->format('H:i:s').$ms;
but be aware of the hour-overflow, if your hours exceed 24 you will probably end up discarding the days.
in your case i would say your approach with floor to get the seconds is correct, and then you should probably just use modulo arithmetics like this:
<?
$totalsecs = 86400*10;
$secs = $totalsecs%60;
echo "secs: $secs \n";
$minutes = ($totalsecs - $secs) % (60*60);
?>
and so on..

Output is in seconds. convert to hh:mm:ss format in php

My output is in the format of 290.52262423327 seconds. How can i change this to 00:04:51?
The same output i want to show in seconds and in HH:MM:SS format, so if it is seconds, i want to show only 290.52 seconds.(only two integers after decimal point)? how can i do this?
I am working in php and the output is present in $time variable. want to change this $time into $newtime with HH:MM:SS and $newsec as 290.52.
Thanks :)
1)
function foo($seconds) {
$t = round($seconds);
return sprintf('%02d:%02d:%02d', ($t/3600),($t/60%60), $t%60);
}
echo foo('290.52262423327'), "\n";
echo foo('9290.52262423327'), "\n";
echo foo(86400+120+6), "\n";
prints
00:04:51
02:34:51
24:02:06
2)
echo round($time, 2);
Try this one
echo gmdate("H:i:s", 90);
For till 23:59:59 hours you can use PHP default function
echo gmdate("H:i:s", 86399);
Which will only return the result till 23:59:59
If your seconds is more then 86399 than
with the help of #VolkerK answer
$time = round($seconds);
echo sprintf('%02d:%02d:%02d', ($time/3600),($time/60%60), $time%60);
will be the best options to use ...
Edit: A comment pointed out that the previous answer fails if the number of seconds exceeds a day (86400 seconds). Here's an updated version. The OP did not specify this requirement so this may be implemented differently than the OP might expect, and there may be much better answers here already. I just couldn't stand having provided an answer with this bug.
$iSecondsIn = 290.52262423327;
// Account for days.
$iDaysOut = 0;
while ($iSecondsIn >= 86400) {
$iDaysOut += 1;
$iSecondsIn -= 86400;
}
// Display number of days if appropriate.
if ($iDaysOut > 0) {
print $iDaysOut.' days and ';
}
// Print the final product.
print date('H:i:s', mktime(0, 0, $iSecondsIn));
The old version, with the bug:
$iSeconds = 290.52262423327;
print date('H:i:s', mktime(0, 0, $iSeconds));
Try this:
$time = 290.52262423327;
echo date("h:i:s", mktime(0,0, round($time) % (24*3600)));
Based on https://stackoverflow.com/a/3534705/4342230, but adding days:
function durationToString($seconds) {
$time = round($seconds);
return sprintf(
'%02dD:%02dH:%02dM:%02dS',
$time / 86400,
($time / 3600) % 24,
($time / 60) % 60,
$time % 60
);
}
I dont know if this is the most efficient way, but if you also need to display days, this works:
function foo($seconds) {
$t = round($seconds);
return sprintf('%02d %02d:%02d:%02d', ($t/86400%24), ($t/3600) -(($t/86400%24)*24),($t/60%60), $t%60);
}
Try this :)
private function conversionTempsEnHms($tempsEnSecondes)
{
$h = floor($tempsEnSecondes / 3600);
$reste_secondes = $tempsEnSecondes - $h * 3600;
$m = floor($reste_secondes / 60);
$reste_secondes = $reste_secondes - $m * 60;
$s = round($reste_secondes, 3);
$s = number_format($s, 3, '.', '');
$h = str_pad($h, 2, '0', STR_PAD_LEFT);
$m = str_pad($m, 2, '0', STR_PAD_LEFT);
$s = str_pad($s, 6, '0', STR_PAD_LEFT);
$temps = $h . ":" . $m . ":" . $s;
return $temps;
}
Personally, going off other peoples answers I made my own parser.
Works with days, hours, minutes and seconds. And should be easy to expand to weeks/months etc.
It works with deserialisation to c# as well
function secondsToTimeInterval($seconds) {
$t = round($seconds);
$days = floor($t/86400);
$day_sec = $days*86400;
$hours = floor( ($t-$day_sec) / (60 * 60) );
$hour_sec = $hours*3600;
$minutes = floor((($t-$day_sec)-$hour_sec)/60);
$min_sec = $minutes*60;
$sec = (($t-$day_sec)-$hour_sec)-$min_sec;
return sprintf('%02d:%02d:%02d:%02d', $days, $hours, $minutes, $sec);
}
1)
$newtime = sprintf( "%02d:%02d:%02d", $time / 3600, $time / 60 % 60, $time % 60 );
2)
$newsec = sprintf( "%.2f", $time );
If you're using Carbon (such as in Laravel), you can do this:
$timeFormatted = \Carbon\Carbon::now()->startOfDay()->addSeconds($seconds)->toTimeString();
But $timeFormatted = date("H:i:s", $seconds); is probably good enough.
Just see caveats.
Here was my implementation with microseconds
/**
* #example 00 d 00 h 00 min 00 sec 005098 ms (0.005098 sec.ms)
*/
public function __toString()
{
// Add your code to get $seconds and $microseconds
$time = round(($seconds + $microseconds), 6, PHP_ROUND_HALF_UP);
return sprintf(
'%02d d %02d h %02d min %02d sec %06d ms (%s sec.ms)',
$time / 86400,
($time / 3600) % 24,
($time / 60) % 60,
$time % 60,
$time * 1000000 % 1000000,
$time
);
}
echo date('H:i:s', round($time)%86400);
Simple formatter with progressively added parts - sample:
formatTime(123) => 2m 3s
formatTime(7400) => 2h 3m 20s
formatTime(999999) => 11d 13h 46m 39s
function formatTime($secs)
{
$secs = max(0, intval($secs));
if($secs > 0){
$out = [];
$yrs = floor($secs / 31536e3);
if($yrs){
$out[] = $yrs."y";
}
$rem = $secs - $yrs * 31536e3;
$days = floor($rem / 86400);
if($days || $out){
$out[] = $days."d";
}
$rem -= $days * 86400;
$hrs = floor($rem / 3600);
if($hrs || $out){
$out[] = $hrs."h";
}
$rem -= $hrs * 3600;
$min = floor($rem / 60);
if($min || $out){
$out[] = $min."m";
}
$rem -= $min * 60;
$out[] = $rem."s";
return implode(" ", $out);
}
return 0;
}
echo date('H:i:s',$time);
echo number_format($time,2);
Numero uno... http://www.ckorp.net/sec2time.php (use this function)
Numero duo... echo round(290.52262423327,2);

Categories