I'd like to split a time entry into its 3 separate variables, ie:
07:15:20
to
$hour = 07
$minute = 15
$second = 20
I know it's a very basic question for most but I'm struggling a bit at the moment :)
<?php
$time = "07:15:20";
list($hour,$minute,$second) = explode(":", $time);
print $hour . " - " . $minute . " - " . $second;
?>
Demo: http://3v4l.org/NWa8L
easiest way I can think of
<?php
$time = "07:15:20";
$time = explode(":",$time);
$hour = $time[0];
$minute = $time[1];
$second = $time[2];
?>
Related
how can I just get the format in minutes instead of for example 01:10:33 to get 70:33, or 00:30:01 to get 30:01.
i get value $sec from array in seconds
["Duration"]=>int(5382) or
["No_ofMinutes"]=> string(4) "89.7"
This is my code,
echo gmdate('H:i:s', $sec);
and I need only ('mm:ss')
thank you all for your help.
You can get minutes like following
gmdate("i")
I think you can generate it from the $sec.
$sec = 1801;
$m = floor($sec / 60);
$s = $sec % 60;
$m = $m < 10 ? '0' . $m : $m;
$s = $s < 10 ? '0' . $s : $m;
$time = $m . ':' . $s;
echo $time;
I have these time value from HTML form using timepicker js.
I want to split the value into three (3) variables which is $hour, $minute and $period.
The problem is I can't split 'AM' or 'PM' value into another variable.
$minute hold the value "00 PM". I want AM or PM store into another variable call $period
What I have done so far as code below.
Time.php
<?php
// $btArray = explode(':', $_POST['app_time']);
$btArray = explode(':', '11:00 PM');
$hour = strlen(trim($btArray[0])) > 1 ? trim($btArray[0]) : '0' . trim($btArray[0]);
$minute = trim($btArray[1]);
$period = trim($btArray[2]);
$query = "UPDATE TABLE
SET
BOOK_TIME = '" . $hour . ":" . $minute . "',
TIME_PERIOD = '" . $period ."'
WHERE
TRACK_NUM = ''
";
?>
My goal is to split the value so I can store it into database.
Appreciate if someone can help me to solve this problem.
Thanks.
$btArray= "7:00 am";
preg_match("/([0-9]{1,2}):([0-9]{1,2}) ([a-zA-Z]+)/", $btArray, $match);
$hour = $match[1];
$min = $match[2];
$period = $match[3];
$query = "UPDATE TABLE
SET
BOOK_TIME = '" . $hour . ":" . $minute . "',
TIME_PERIOD = '" . $period ."'
WHERE
TRACK_NUM = ''
";
Try this,
$time = '11:00 PM';
//first remove right two char
$period = substr($time, -2, 2);
//remove that string from main string
$newTime = str_replace($period, "", $time);
//convert remaining string to array
$btArray = explode(':', $newTime);
//check hour string length and concate 0
$hour = (strlen($btArray[0]) > 1) ? $btArray[0] : '0'.$btArray[0];
$mint = $btArray[1];
echo $hour.'<pre>';
echo $mint.'<pre>';
echo $period.'<pre>';
die;
Output:
11
00
PM
I would rather suggest to use substr() function as the parameters like: hour, minute, seconds are fixed.
First need to check whether the string length of 8.
Then, get parameters depending upon their positions.
If your date format is constant.
<?php
$str = '11:00 PM';
if (strlen($str) == 8) {
$hour = substr($str, 0, 2);
$minute = substr($str, 3, 2);
$period = substr($str, 6, 2);
}
echo '<pre>';print_r('Hour: '. $hour);echo '</pre>';
echo '<pre>';print_r('Minute: '. $minute);echo '</pre>';
echo '<pre>';print_r('AM/PM: '. $period);echo '</pre>';
Output:
Hour: 11
Minute: 00
AM/PM: PM
Working Demo:
You can try below code for your desire output.
$btArray = explode(':', '11:00 PM');
$hour = strlen(trim($btArray[0])) > 1 ? trim($btArray[0]) : '0' . trim($btArray[0]);
$minute_period_array = explode(' ', $btArray[1]);
$minute = trim($minute_period_array[0]);
$period = trim($minute_period_array[1]);
echo 'Hour - ' . $hour . '<br/>';
echo 'Minute - ' . $minute . '<br/>';
echo 'Period - ' . $period . '<br/>';
Output :
Hour - 11
Minute - 00
Period - PM
If you will get the input value always in the same format, you can go this way:
$input = '8:05 PM';
list($hour, $minute, $period) = explode(' ', str_replace(':', ' ', $input), 3);
// And then clean the variables
$hour = str_pad($hour, 2, '0', STR_PAD_LEFT);
$minute = str_pad($minute, 2, '0', STR_PAD_LEFT);
$period = strtoupper(trim($period));
// $hour -> 08, $minute -> 05, $period -> 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
I would like to ask of this kind of double data type can be converted into seconds?
In my database, the record is 6.80 means that 6 is hours and 80 is minutes. I want the output be 7 hours and 20 minutes. Is this possible?
Do the OOP way..!
For a 12-Hr Format.
<?php
$dt='6.80';
$date = DateTime::createFromFormat('H.i', $dt);
echo $date->format('g')." hours and ".$date->format('i')." minutes";
OUTPUT:
7 hours and 20 minutes
For a 24-Hr Format.
<?php
$dt='23.75';
$date = DateTime::createFromFormat('H.i', $dt);
$var= ($date->format('G')==0)?'00':$date->format('G');
echo $var." hours and ".$date->format('i')." minutes";
OUTPUT:
00 hours and 15 minutes
Example:
$t=6.80;
$s = $t - floor($t);
if($s>.60)
{
$t = $t -$s;
$t++;
$s=$s-.60;
$t=$t+$s;
}
floor() function is used to get the fractional part of a floating point number.
Try this
$str = "6.80";
$str = explode(".",$str);
$min = $str[0]*60 + $str[1];
$sec = $min*60;
echo date("H:i",$sec);
(Or)
$hour = intval($min/60);
$mins = $min%60;
echo "$hour:$mins";
this is another way:
<?php
$time = '9.102';
$tmp_min = strstr($time, '.');
$tmp_hour = strstr($time, '.', true);
$pos = strpos($time, '.');
$str_mins = substr($time, $pos+1);
$min = (int) ($str_mins);
if ( $min > 60 ) {
$ext_hour = floor($min/60);
$final_min = $min%60;
$final_hour = (int) ($tmp_hour + $ext_hour);
echo $final_hour . ' Hours and ' . $final_min . ' Minuts..';
}
else {
$final_hour = (int) ($tmp_hour);
$final_min = $min;
echo $final_hour . ' Hours and ' . $final_min . ' Minuts..';
}
?>
Can anyone help me to get hour, minutes, seconds from this code:
<strong class="countdown"> 23:16:27</strong>
I have tried with this code to get data:
$r = sscanf($str, "%f,%f,%f", $hour, $minutes, $secundes);
How to parse this countdown timer with regex or something else, and put into $hour, $minutes, $seconds? (sorry for my English)
Use strtotime(); see here:
<?php
$time = '23:16:27';
$time = strtotime($time);
$hour = date('g',$time);
$minute = date('i',$time);
$second = date('s',$time);
echo "hour: " . $hour . "<br>";
echo "minute: " . $minute . "<br>";
echo "second: " . $second . "<br>";
?>
if (preg_match('/[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}/i', $str, $m)) {
$hours = $m[1];
$minutes = $m[2];
$seconds = $m[3];
}