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
Related
I have 2 string like this "2018/04/10-14:54:55" and "2018/04/10-14:56:10".
How can I calculate different between them? I've used strtotime to change the string to date but it gives the wrong value.
Please help me with this.
Thank you
<?php
$time1 = '2018/04/10-14:54:55';
$time2 = '2018/04/10-14:56:10';
$format = 'Y/m/d-H:i:s';
$t1 = DateTime::createFromFormat($format, $time1);
$t2 = DateTime::createFromFormat($format, $time2);
echo "time1" . $t1->format('Y-m-d H:i:s') . "<br/>";
echo "time2" . $t2->format('Y-m-d H:i:s') . "<br/>";
$difference = $t1->diff($t2);
echo $difference->format('%s%a secs');
You can use specific format to create a datetime ojbect, after that you can use diff function to get the difference.
Please try this code.
function time_difference($time_1, $time_2, $limit = null)
{
$val_1 = new DateTime($time_1);
$val_2 = new DateTime($time_2);
$interval = $val_1->diff($val_2);
$output = array(
"year" => $interval->y,
"month" => $interval->m,
"day" => $interval->d,
"hour" => $interval->h,
"minute" => $interval->i,
"second" => $interval->s
);
$return = "";
foreach ($output AS $key => $value) {
if ($value == 1)
$return .= $value . " " . $key . " ";
elseif ($value >= 1)
$return .= $value . " " . $key . "s ";
if ($key == $limit)
return trim($return);
}
return trim($return);
}
$time1 = '2018/04/10 14:54:55';
$time2 = '2018/04/10 14:56:10';
$resp = time_difference ($time1, $time2);
echo $resp;
output:
1 minute 15 seconds
This is an example, I have explained the details in the code itself:
<?php
date_default_timezone_set("Asia/Tehran"); //Set your timezone to avoid warnings in PHP error_log
// Use '/' for American date format and use '-' for Europian date format
// Use 'T' instead of '-'
$time_1 = strtotime( str_replace('-', 'T', "2018/04/10-14:54:55") ); //Replace '-' to 'T' to get "2018/04/10T14:54:55"
$time_2 = strtotime( str_replace('-', 'T', "2018/04/10-14:56:10") ); //Replace '-' to 'T' to get "2018/04/10T14:56:10"
$diff = $time_2 - $time_1;
echo $diff; // in seconds
?>
I hope it helps.
I'm using 2 variables: check-in, check-out
Their format is: dd/mm/yyyy
I want separate the day, month and year into single variables,
in order to compare the integers of:
dd(check-in) with dd(check-out),
mm(check-in) with mm(check-out), and
aaaa(check-in) with aaaa(check-out),
so that the check-in cannot be done after the check-out, and an error appears when somebody try.
On the php file:
$in_date = $_POST['check-in'];
settype( $in_date, "string");
$in_yyyy = substr( $in_date , 6, 4);
settype( $in_yyyy, "integer");
$in_mm = substr( $in_date , 3, 2);
settype( $in_mm, "integer");
$in_dd = substr( $in_date , 0, 2);
settype( $in_dd, "integer");
$out_date = $_POST['check-out'];
settype( $out_date, "string");
$out_yyyy = substr( $out_date , 6, 4);
settype( $out_yyyy, "integer");
$out_mm = substr( $out_date , 3, 2);
settype( $out_mm, "integer");
$out_dd = substr( $out_date , 0, 2);
settype( $out_dd, "integer");
Suppose I typed in input:
check-in: 16/12/2016
check-out: 23/01/2017
However, those are the values the variables takes on:
$in_date = 16/12/2016
$in_yyyy = 2017 // THAT'S WRONG
$in_mm = 01 // THAT'S WRONG
$in_dd =23 // THAT'S WRONG
$out_date = 23/01/2017
$out_yyyy = 2017
$out_mm = 01
$out_dd =23
The problem seems to be caused by "substr". It seems to take on, no matter what, always the last value attributed to it.
Any suggestion?
Assuming you always get the date in the below format, you can use the explode() function in PHP to separate the day, month and year from the date.
$date = '16/12/2016';
$parts = explode('/', '', $date);
$day = $parts[0];
$month = $parts[1];
$year = $parts[2];
unset($parts); // clean up
Your code may look something similar to:
$dates = array(
'check_in' => $_POST['check_in'],
'check_out' => $_POST['check_out']
);
foreach($dates as $_date)
{
settype( $_date, "string");
$parts = explode('/', '', $_date);
$day = $parts[0];
$month = $parts[1];
$year = $parts[2];
settype( $day, "integer");
settype( $month, "integer");
settype( $year, "integer");
}
unset($_date); // clean up
I would strongly recommend that you utilize it with Datetime object when you are working with date/time although explode() or substr() will also work.
$dt = DateTime::createFromFormat('d/m/Y', '16/12/2016');
echo "Year: " . intval($dt->format('Y')) . "\n";
echo "Month: " . intval($dt->format('m')) . "\n";
echo "Day: " . intval($dt->format('d')) . "\n";
Benefit of it is that also you can use this datetime objects to compare your check-in and check-out way easier, or you can even easily calculate nights etc. Try following code:
<?php
$dt = DateTime::createFromFormat('d/m/Y', '16/12/2016');
echo "Year: " . intval($dt->format('Y')) . "\n";
echo "Month: " . intval($dt->format('m')) . "\n";
echo "Day: " . intval($dt->format('d')) . "\n";
$dt_out = DateTime::createFromFormat('d/m/Y', '10/12/2016');
if ($dt > $dt_out) {
echo "Check-in cannot be after check-out\n";
}
$dt_out = DateTime::createFromFormat('d/m/Y', '18/12/2016');
if ($dt < $dt_out) {
echo "This is good!\n";
}
echo "Nights: " . $dt->diff($dt_out)->format("%a") . "\n";
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];
?>
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];
}