Combine two array in php - php

I am new in php. I want to combine two array data into one table. My first array is given below:
http://i.imgur.com/XmVaUPH.png
My second array is given below :
http://i.imgur.com/jSOdlMD.png
My output will be:
Name Date Start time End time
Billal 1-14-15 11:29 AM 8:30 Pm
Emrul 1-14-15 2:21 PM 8:34 pm
Limon 1-14-15 11:26 AM
Mamun 1-14-15 11:47 AM 8:32 pm
Masum 1-14-15 12:12 PM 8:33 pm
Shahed 1-14-15 11:30 Am
Emrul 1-15-15 11:29 AM 8:30 Pm
Limon 1-15-15 11:47 AM 8:32 pm
Masum 1-15-15 12:12 PM 8:33 pm
Here start time will be 10 am to 2 pm and rest are end time. when start/end time two data found fast data will be counted

Looks like what you need here is to combine two arrays you have given and order the result based on the time taken.
Here are some best practices to approach your problem.
If you ever need to calculate date/time do not use strings, use timestamps because it is just a one big number which you can use to easily manipulate (in your case you just need to subtract start date/time from end date/time to get the total time taken) using simple math.
If you store the data in a SQL database then don't retrieve them and calculate the time taken in PHP, instead calculate the durations using SQL before retrieving data. It will massively reduce the application overhead. You also can use SQL sorting to sort your result set.
Please don't attach screenshots of your xdebug output in future, instead paste on to the editor.

$array1a = array_chunk($array1, 3);
$array2a = array_chunk($array2, 3);
$table =array();
foreach($array1a as $v1){
$tmp = array(
'name'=>$v1[0],
'date' => $v1[1],
'time1' => $v1[2]
);
foreach($array2a as $v2){
if($v2[1] == $v1[1] && $v2[0] == $v1[0]){
$tmp['time2']=$v2[2];
}
}
$table[] = $tmp;
}
hope it help

array_merge() is an useful function for merging two arrays. Here is a simple example.
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>

Related

How to calculate time in HH:mm:ss format from array

I m trying to calculate time from an array where time is stored in HH:mm:ss format, i m making time array from this code,
$result=[];
foreach ($playerTimes as $playerTime) {
$playerStartTime = (isset($playerTime->start_time))? $playerTime->start_time: '00:00:00';
$playerEndTime = (isset($playerTime->End_time))? $playerTime->End_time: '00:00:00';
$result[] = \Carbon\Carbon::parse($playerStartTime)->diff(\Carbon\Carbon::parse($playerEndTime))->format('%H:%I:%S');
}
and I get this array
array:3 [0 => "08:44:14" 1 => "10:46:16" 2 => "12:48:18"]
the result I m trying to achieve after the addition is 32:18:48
I have tried this, however the result I got is 08:18:48 which is not what i need,
$result[] = \Carbon\Carbon::parse($playerStartTime)->diffInSeconds(\Carbon\Carbon::parse($playerEndTime));
dd(gmdate("H:i:s", array_sum($result)));
The problem you are running into is that the H:i:s format does just not support what you are trying to achieve here, as H can never be greater than 23, nor smaller than 0.
There are two possible solutions: you can use Y-m-d H:i:s, to show the difference in years, months and days also, or you can use these values to convert them back to hours, or calculate the hours from the seconds difference (so seconds difference / 60 / 60 = hours), and work from there.

Compare and find the closest datetime to given ending datetime

I have a list of data that I've retrieved and is ordered from previous to more recent datetime. I'm trying to loop backwards in this list based on a time and date I specify, however the date and time in the list won't necessarily be equal to the datetime I specify so I want it to find the closest datetime in the list then iterate backwards. Here's an example:
I want 2016-08-27 23:30:00 PM.
List:
2016-08-27 22:30:00 PM
2016-08-27 22:30:20 PM
2016-08-27 23:29:40 PM
2016-08-27 23:29:59 PM
2016-08-27 23:29:58 PM
How can I do this with PHP?
Push dates into array and sort it.
$array = ['2016-08-27 23:29:58 PM', '2016-08-27 23:29:59 PM'];
function sortbydate( $a, $b ) {
return strtotime($a) - strtotime($b);
}
usort($array, "sortbydate");
var_dump($data);
If you want remove newer dates
function new($var)
{
return strtotime($a) < strtotime('2016-08-27 23:29:59');
}
array_filter($data, "new");

How to generate registration no. in PHP?

I want generate registration no of user but registration no should be:
First two digit no. should be year as - 15
Second two digit no. should be month as - 06
Third two should be Character which already assign as - SY
Fourth should be No as - 012
So my registration no will make as - 1506SY012. How to generate it in PHP code?
Seeing this question is still open, I will post the following as a solution and there are a few ways you can go about this.
Explode the date and separate them as an array and assigning a variable to each array.
Use date directly
Concatenate the variables.
Using the date() function - http://php.net/manual/en/function.date.php
y = A two digit representation of a year. Examples: 99 or 03
m = Numeric representation of a month, with leading zeros. 01 through 12
Explode on the date:
$date1 = date("y-m-d");
$dateArray=explode('-',$date1);
$array1 = $dateArray[0];
echo $array1;
$array2 = $dateArray[1];
echo $array2;
// Should you want to use the third array
// $array3 = $dateArray[2];
// echo $array3;
Sidenote for the above: You can concatenate (link together) the variables into one, rather than echoing them seperately. I will let you do that.
Using the date directly and concatenating:
$date = date("ym");
$final = $date . "SY" . "012";
echo $final;
or
$date = date("ym");
$letters = "SY";
$numbers = "012";
$result = "$date$letters$numbers";
echo $result;
References:
http://php.net/manual/en/function.date.php
http://php.net/explode

Using regex to group data and it's children

I have a simple document that I need to split up into events (by day), unfortunately the document contains other useless info (such as event details) which I'll need to crawl through to retrieve the info. An except of this document looks like this:
10th March 2015
Baseball 10:00 Please remember to bring your bats
Soccer 14:00 over 18s only
11th March 2015
Swimming 10:00 Children only
Soccer 14:00 Over 14s team training
My initial plan was to use preg_spit to try and split the string at the date, then loop over each one, however I need to maintain the structure of the document.
Ideally I'd like to return the data into an array like:
arr[
'days' =>[
'date' => '10th MArch 2015'
'events' => ['Baseball 10:00', 'Soccer 14:00'],
]
]
How would I best go about doing this? Regex isn't my strongest suit, but I know enough to capture the days ([0-9]{1,2}[a-z]{2}/s[a-z]+/s[0-9]{4}) and the events ([a-Z]+/s[0-9]{2}:[0-9]{2}).
You can use this regex:
/(?:\b(\d+th\h+.*?\d{4})\b|\G)\s+(\S+\h+\d{2}:\d{2}\b).*?(?=\s+(?>\S+\h+\d{2}:\d{2}|\d+th\h+|\z))/i
And then a bit of PHP code to loop through the result.
RegEx Demo
This is what I came up with. I used explode() to split out the different sections and then to split up the lines. I didn't use preg_match() until the very end to get the specific sport/time.
<?php
$text = <<<EOD
10th March 2015
Baseball 10:00 Please remember to bring your bats
Soccer 14:00 over 18s only
11th March 2015
Swimming 10:00 Children only
Soccer 14:00 Over 14s team training
EOD;
$days = array();
if( $sections = explode("\n\n",$text) ){
foreach($sections as $k=>$section){
$events = array();
$lines = explode("\n",$section);
$day = $lines[0];
unset($lines[0]);
if($lines){
foreach($lines as $line){
preg_match("/(\w+)\s(\d){2}:(\d){2}/",$line,$matches);
if(isset($matches[0])){
$events[] = $matches[0];
}
}
}
$days[$k] = array(
'day' => $day,
'events' => $events
);
}
}
echo '<pre>',print_r($days),'</pre>';

How to remove to expiry time in php using mysql database

How to remove the expiry time in php using mysql database....
I want to remove the expiry time which are derived from mysql db..In my code i want to compare the show time from database with current time,to get the showtime which is greater than the current time.(Eg), showtime="10:30 AM,2:30 PM,6:30 PM,9:30 PM" and curent time is 03:21 PM.
Getting output:
{"offer_details":[{"showtime":["10:30 AM","2:30 PM","6:30 PM","9:30 PM"]}],"success":1,"message":"Successfully found "}
Expected output:
{"offer_details":[{"showtime":["6:30 PM","9:30 PM"]}],"success":1,"message":"Successfully found "}
$result=mysql_query("select * from offer_details where movieid='$movieid' and date_value='$date_value' and offer_status!='Expired'")or die (mysql_error());
$offer_details=array();
$offer_details['showtime']=explode(',',$row['showtime']);
$av = sizeof($offer_details['showtime']);
echo $showtime[i];
Thanks,
You can compare time via hour like this:
foreach($offer_details['showtime'] as $key=>$showTime){
if(date("H", strtotime($showTime)) < date("H", strtotime("now"))){
unset($offer_details['showtime'][$key]);
}
}
array_values($offer_details['showtime']);
When you explode the showtime string you actually obtain strings which are formatted as times but remain as strings. That is why you cannot get the result when you compare with the current time as strings ("8:00 AM" will always be greater than "3:00 PM"). I suggest you store the showtimes as different fields in your database (with datetime types) so that you only pick those greater than now. I hope it helps.
Try this example and see if this helps.
<?php
$offer_details['showtime']= ["10:30 AM","2:30 PM","6:30 PM","9:30 PM"];
//$now = strtotime('now');
$now = strtotime('3:30 PM');
$showtimes = array();
foreach($offer_details['showtime'] as $showtime) {
if(strtotime($showtime) >= $now) {
$showtimes[] = $showtime;
}
}
print_r($showtimes); //outputs Array ( [0] => 6:30 PM [1] => 9:30 PM )

Categories