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';
Related
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
I'm creating a countdown for how long people can do specific functions on a specific page. I've made a countdown, but he is going under 0, so he's counting further negatively.
I'm printing out the countdown like this:
-4 days -10 h -3 m -34 s remaining to run 1210 meter
My countdown,
$Date = "" . $groups['group_date'] . "";
$stopDate = date('Y-m-d H:i:s', strtotime($Date. ' +' . $periodOfRunning . 'days'));
<h3><?php $rem = strtotime($stopDate) - time();
$day = floor($rem / 86400);
$hr = floor(($rem % 86400) / 3600);
$min = floor(($rem % 3600) / 60);
$sec = ($rem % 60);
if($day) echo "$day days ";
if($hr) echo "$hr h ";
if($min) echo "$min m ";
if($sec) echo "$sec s";
echo " remaining to run " . $totalDistanceToDo . " meter";
?></h3>
If you use DateTime() you can compare the two times and see if the stop time is in the past. If it is, display a different message. Also, this makes getting the interval between the two easier to get and display:
$now = new DateTime();
$stopdate = new DateTime($stopDate);
if ($stopdate > $now) {
$diff = $now->diff($stopTime);
echo $diff->format('%d days, %h hours, %i minutes, %s seconds');
echo " remaining to run " . $totalDistanceToDo . " meter";
}
else {
// negative time. display something else
}
Keep in mind if you don't want to display zero values for time units it will be a little more complex:
$now = new DateTime();
$stopdate = new DateTime($stopDate);
if ($stopdate > $now) {
$diff = $now->diff($stopTime);
if ($diff->d > 0) echo $diff->d . ' days, ';
if ($diff->h > 0) echo $diff->h . ' hours, ';
if ($diff->i > 0) echo $diff->i . ' minutes, ';
if ($diff->s > 0) echo $diff->s . ' seconds';
echo " remaining to run " . $totalDistanceToDo . " meter";
}
else {
// negative time. display something else
}
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..';
}
?>
Is there any php function where i can get exact date, by providing week number of the month and day as,
function required_date($week_num, $day) {
// should return 20th of September, if i pass (3, 'Friday')
}
I am getting week number of the month as
ceil(substr(date('Y-m-d'), -2) / 7);
and day as,
date('l');
Any help, please!
function required_date($week_num, $day) {
$week_of_year = sprintf('%02d', date('W', strtotime(date('Y-m-01'))) + $week_num);
$day_of_week = date('N', strtotime($day));
$timestamp = strtotime(date('Y') . '-W' . $week_of_year . '-' . $day_of_week);
return $timestamp;
}
$timestamp = required_date(3, 'friday');
echo date('Y-m-d', $timestamp);
try this:
date('Y-m-d', strtotime('W' . $week_num . ' ' . $day));
How about
function required_date($week_num, $day)
{
return date('Y-m-') . substr('0' . (($week_num - 1) * 7 + date('N', strtotime($day))) + 1, -2);
}
var_dump(required_date(3, 'Friday'));
var_dump(required_date(4, 'Friday'));
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];
}