Parsing time from HTML string - php

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];
}

Related

Minute format php

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;

How to split time value in PHP?

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

Splite STring get error Offcet

I'm trying to split a string that contains a time like this "23:25:00"
I want to get just hours and minutes but it gives an error like offset ...
I tried this :
$time= "12:12:50";
$time = explode(":", $time);
echo $time[0]; // Hours
echo $time[1]; // minutes
$time = '12:23:59';
$timeParts = explode(':', $time);
echo $timeParts[0] . ': hours';
echo $timeParts[1] . ': minutes';
echo $timeParts[2] . ': seconds';

Split MySQL 'TIME' field into separate parts

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];
?>

Convert into Seconds from Double Data Type

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..';
}
?>

Categories