Counting a specific variable within an array - php

I have an array of data with a startDate variable - How would I go about returning the total number of objects with the variable of date and then just print the number?
$date = Todays date
foreach ($results->data as $row) {
$checkDate = $row->startDate;
if(date('Y-m-d', strtotime($row->startDate)) == $date){
Return total of date found
}
}

Just increment a counter whenever it is found:
$date = '2019-02-20';
$total = 0;
foreach ($results->data as $row) {
if(date('Y-m-d', strtotime($row->startDate)) == $date){
$total++; // same as $total = $total + 1;
}
}
echo "$date found $total times";

Related

Extracting dates from date range

Part of my answer was answered by dynamic:
How to extract every Monday and every fortnightly Monday from a range of two dates in PHP?
I've added a foreach loop to print all the mondays in this example:
$start = strtotime('2016-09-05');
$end = strtotime('2016-09-30');
$mondays=array();
$tuesdays=array();
while( $start <= $end ) {
if ( date('N',$start)==1 )
$mondays[]=$start;
$start += 86400;
}
foreach ($mondays as $item) {
print date("D. Y-m-d", ($item));
print ("<br>");
}
and i get these results:
enter image description here
But how do i add an else/if statements to display the tuesdays for example
if the date begins with $start = strtotime('2016-09-06');
I want it to show:
enter image description here
Thanks.
In this code, we first find the DayOfWeek, and then find all dof in range:
// Data
$StartDate = strtotime('2016-09-06');
$EndDate = strtotime('2016-09-30');
$DayOfWeek = date("l", $StartDate); // Determine day of week
$LoopDate = $StartDate;
// Find all DayOfWeek's Between Start and End
$Result = [$StartDate];
while (true) {
$NextDayOfWeek = strtotime("next " . $DayOfWeek, $LoopDate);
if ($NextDayOfWeek > $EndDate) {
break;
}
$Result[] = $LoopDate = $NextDayOfWeek;
}
// Print Result
/**
| 2016-09-06
| 2016-09-13
| 2016-09-20
| 2016-09-27
*/
foreach( $Result as $Row ) {
echo date("Y-m-d", $Row) . '<br/>';
}
In keeping with your current approach:
$start = strtotime('2016-09-05');
$end = strtotime('2016-09-30');
while($start <= $end) {
if(date("D", $start) == "Mon"){
$mondays[] = $start;
}
elseif(date("D", $start) == "Tue"){
$tuesdays[] = $start;
}
$start += 86400;
}
foreach ($mondays as $item){
echo date("D. Y-m-d", $item);
echo "<br>";
}
echo "<br>";
foreach ($tuesdays as $item){
echo date("D. Y-m-d", $item);
echo "<br>";
}

check if variable is higher than item in array php

I want to check if a variable is greater than or equal to an item in an array.
I've tried multiple for loops, but then they just stack and stack and stack. I just need to get the dates, for example: 01-01-2005 / 01-01-2006
and compare those two both with today's date and check if today's date is greater then or equal to the date of the book. And if the book is older then today's date do some code
foreach ($user_out as $userOut) {
$userOut_id[] = $userOut['user_id'];
}
$userOut_id_implode = implode(',',$userOut_id);
//loop through each element from the tocomet able
$dateOfBook = [];
$today = date("d-m-Y");
foreach ($out as $outs) {
$datum = $outs['released'];
//gets the date of the selected book.
$bookDate = date("d-m-Y", strtotime($datum));
$dateOfBook[] = $outs['released'];
//gets the name of the author
$author_name = $outs['author'];
//gets the title of the book
$book_title = $outs['book_title'];
$today = strtotime($today);
$bookDate = strtotime($bookDate);
//checks if today is equal or smaller then the bookdate
}
$today = date("Y-m-d");
$y = count($dateOfBook);
for($x = 0; $x < $y; $x++){
echo $x;
};
if($today >= $dateOfBook($x)){
do stuff
}
$dateOfBook = [];
$today = date("d-m-Y");
$flag = 0;
foreach ($out as $outs) {
//gets the date of the book out of the database
if($today >= $outs['released']){
$flag++;
}
$dateOfBook[] = $outs['released']; # is this necessary?
}
if($flag){
# do something
}

Split the data into three arrays based on the time

I want to split the data array into three arrays - past, today, later - based on the time.
For today's array, only data with today's date and the time greater than today's time should be there.
Here's my code. There is no bug in it (as far as I know). I am getting the desired result, but I want to know if this code can be more optimized.
$current_date = date('Y-m-d');
$current_time = date('H:i:s');
foreach($data as $key => $value) {
if(date('Y-m-d', $value->meeting_scheduled_at) > $current_date) {
$calendar_data['later'][] = $value;
} elseif (date('Y-m-d', $value->meeting_scheduled_at) < $current_date
|| ( (date('Y-m-d', $value->meeting_scheduled_at) == $current_date)
&& (date('H:i:s', $value->meeting_scheduled_at) < $current_time) )
) {
$calendar_data['past'][] = $value;
} else {
$calendar_data['now'][] = $value;
}
}
$value->meeting_scheduled_at is in timestamp
EDIT: I am using this in a Laravel project.
So, if the meeting is scheduled at today's date, and time is evening 6 PM. It will come into today array till the 6 PM. After 6 PM, it goes into the past array. In short, nothing should be there in later array, unless and until it is next day.
I like to use the DateTime class for this kinda thing. It takes into account daylight savings etc and can be compared really easily.
$now = new DateTime();
$tomorrow = new DateTime();
$meetingDate = new DateTime();
$tomorrow->add(new DateInterval('P1D'))->setTime(0,0,0);
foreach ($data as $key => $value) {
$meetingDate->setTimestamp($value->meeting_scheduled_at);
// if $value->meeting_scheduled_at format YYYY-MM-DD then
// we'd create the $meetingDate object here like this
// $meetingDate = new DateTime($value->meeting_scheduled_at);
if ($meetingDate < $now) {
$calendar_data['past'][] = $value;
} else if ($meetingDate >= $tomorrow) {
$calendar_data['later'][] = $value;
} else {
$calendar_data['now'][] = $value;
}
}
date('Y-m-d', $value->meeting_scheduled_at) should be calculated only once before you use its result in the conditions.
Make it easier
$now = time();
// Midnight due to discussion with Kevin Nagurski in comments :)
$tommorow = strtotime('tomorrow');
foreach($data as $key => $value)
if ($value->meeting_scheduled_at) < $now) $calendar_data['past'][] = $value;
elseif ($value->meeting_scheduled_at) >= $tommorow) $calendar_data['later'][] = $value;
else $calendar_data['now'][] = $value;
You can use timestamp instead.
$now = time();
$today_limt = strtotime("today 6 PM");
foreach($data as $key => $value) {
$meeting_time = strtotime($value->meeting_scheduled_at);
if($meeting_time > $today_limit) {
$calendar_data['later'][] = $value;
} elseif($meeting_time < $now) {
$calendar_data['past'][] = $value;
} else {
$calendar_data['now'][] = $value;
}
}

I have an issue with my loops to show the result value

$array_of_time = array ();
$start_time = strtotime ("$app_date 07:00");
$end_time = strtotime ("$app_date 22:00");
$mins = 04 * 60;
while ($start_time <= $end_time)
{
$array_of_time[] = date ('h:i a', $start_time);
$start_time += $mins;
}
echo "<div style='width:700px' class='time_slot_div'>";
echo "<p class='time_slot_p'> Time Slot :</p>";
foreach($array_of_time as $key=>$value)
{
for($i = 0; $i < count($app_data); $i++)
{
$book_time=$app_data[$i]["appointment_time"];
if($value==$book_time)
{
echo "<a class='time_slot_a_book'>$value</a> ";
} else {
echo "<a href='#' onclick='get_time_value(\"$value\");' class='time_slot_a'>$value</a> ";
}
}
}
echo "</div>";
Here foreach loop can run as many time as it can as well as for loop also, but i want to show the links that are not matched with the foreach value and for loop value.
The foreach loop values like 7:20 am not from database but the for loop value like 7:20 am is from database so if 7:20 am==7:20 am then the if statement it run it is working fine, but the issue is that it is running 2 time if i get 2 value in for loop. It should run my div only once.
If took a tour of your site, I revise the copy:
$array_of_time = array ();
$start_time = strtotime ("$app_date 07:00");
$end_time = strtotime ("$app_date 22:00");
$mins = 04 * 60;
while ($start_time <= $end_time)
{
$array_of_time[] = date ('h:i a', $start_time);
$start_time += $mins;
}
echo "<div style='width:700px' class='time_slot_div'>";
echo "<p class='time_slot_p'> Time Slot :</p>";
foreach($array_of_time as $key=>$value)
{
//
// get the appointing state:
//
$bAppointed = false;
for($i = 0; $i < count($app_data); $i++)
{
$book_time = $app_data[$i]["appointment_time"];
$bAppointed = $value == $book_time;
if($bAppointed)
{
break;
}
}
//
// print the time slot:
//
if($bAppointed)
{
echo "<a class='time_slot_a_book'>$value</a> ";
} else {
echo "<a href='#' onclick='get_time_value(\"$value\");' class='time_slot_a'>$value</a> ";
}
}
echo "</div>";
?
Is it better ?

How can I retrieve the output of a php foreach statement as a variable?

Proving to myself I'm still a total PHP newbie, I really could use some help. I have the following code (bottom) and it's providing me the data I need in this format:
2012-11-01;;1;;200, 2012-11-02;;1;;200, 2012-11-03;;1;;200, 2012-11-04;;1;;200, 2012-11-04;;1;;200, 2012-11-05;;1;;200, 2012-11-06;;1;;200, ...etc...
But the problem is I need the total output of the foreach statement to be set as a variable. I know I've been SO close, but I keep messing it up. How can I set a variable to have the same value as the total output of the foreach statement that still looks like the above output when echo'd out?
function getDatesBetween2Dates($startTime, $endTime) {
$day = 86400;
$format = 'Y-m-d';
$startTime = strtotime($startTime);
$endTime = strtotime($endTime);
$numDays = round(($endTime - $startTime) / $day) + 1; // + 1
$days = array();
for ($i = 0; $i < $numDays; $i++) {
$days[] = date($format, ($startTime + ($i * $day)));
}
return $days;
}
$days = getDatesBetween2Dates('2012-11-01', '2012-11-30');
foreach($days as $key => $value){
echo $value.";;1;;200,\n";
}
Something like this
$value='';
foreach($days as $key => $val){
$value.=$val.";";
}
echo $value; // Output will be 2012-11-01;2012-11-02;2012-11-03; and more...
DEMO.

Categories