PHP - Strtotime Issue With Foreach
This kind of has me in lala land as this is very strange...
I'm going through a list of dates in such format. Code: [Select] [earned] => Array ( [0] => 2010-07-14T11:26:09Z [1] => 2010-07-31T06:35:14Z [2] => 2010-07-14T11:25:55Z [3] => 2010-07-31T06:29:43Z [4] => 2010-07-31T06:31:47Z [5] => 2010-07-14T11:26:17Z ) Everything is setup correctly but the first offset of an array comes back blank. Like so. - 2010-07-14T11:26:09Z 1280558114 - 2010-07-31T06:35:14Z 1279106755 - 2010-07-14T11:25:55Z 1280557783 - 2010-07-31T06:29:43Z 1280557907 - 2010-07-31T06:31:47Z 1279106777 - 2010-07-14T11:26:17Z Now if I manually use strtotime on just 2010-07-14T11:26:09Z I get the value I seek (1279106769) My foreach setup... Code: [Select] foreach($trophy['earned'] as $key2=>$value2) { echo strtotime($trophy['earned'][$key2]) . " - " . $trophy['earned'][$key2] . "<br />"; } Any ideas? Similar Tutorialshi phpfreaks In short I don't know what I am doing wrong. I have two arrays that preg_match_all then the results from that are placed into variables. The $valuematch is working fine but the $valuepath won't they print_r correctly but when I run the script and look at the source I find the option name shows nothing. Code: [Select] private function generateSelect ($menuString) { $return ='<select>'; preg_match_all('/href="(.*?)"/', $menuString, $path); print_r($path); preg_match_all('/title="(.*?)"/', $menuString, $matches); print_r($matches); foreach ($matches[1] as $key => $valueMatch) { $path[1] = $valuePath; $return .= "<option name='".$valuePath."'>".$valueMatch."</option>"; } $return.='</select>'; $this->selectOption =$return ; } I'm trying to figure out why its only showing the last child_links in the roster, events, and social objects. I've included the site that has the print_r of the array function. Any help would be appreciated. http://kansasoutlawwrestling.com/ Code: [Select] function getSubMenuPages() { $this->db->select('site_menu_structures.id'); $this->db->from('site_menu_structures'); $this->db->where('site_menu_structures.short_name', 'mainnav'); $query = $this->db->get(); $menu_id = $query->row()->id; $this->db->select('site_menu_structures_links.id, site_menu_structures_links.short_name, site_menu_structures_links.is_category'); $this->db->from('site_menu_structures_links'); $this->db->where('site_menu_structures_links.menu_structure_id', $menu_id); $query = $this->db->get(); if ($query->num_rows() > 0) { $linksArray = $query->result(); foreach ($linksArray as $key => $link) { if ($link->is_category == 'Yes') { $this->db->select('site_menu_structures_links_children.link_name, site_menu_structures_links_children.site_content_pages_id, site_menu_structures_links_children.link_url'); $this->db->from('site_menu_structures_links_children'); $this->db->where('site_menu_structures_links_children.site_menu_structures_links_id', $link->id); $query = $this->db->get(); if ($query->num_rows() > 0) { foreach ($query->result() as $row) { $site_content_page_id = $row->site_content_pages_id; $linksArray[$key]->child_links = array(); if ($site_content_page_id != 0) { $this->db->select('site_content_pages.content_page_name, site_content_pages.permalink'); $this->db->from('site_content_pages'); $this->db->where('site_content_pages.id', $site_content_page_id); $query = $this->db->get(); if ($query->num_rows() > 0) { $row = $query->row(); $linksArray[$key]->child_links = array( 'link_name' => $row->content_page_name, 'link_url' => $row->permalink ); } } else { $linksArray[$key]->child_links = array( 'link_name' => $row->link_name, 'link_url' => $row->link_url ); } } } } } } return $linksArray; } Hello I'm having problems regarding coding right the following problem One class (students) can have class all together or/and divided (haveDesdobramento) into 2 shifts So in the beginning i'm choosing all class and defined 100 hours Using a foreach because i need to check holidays, school breaks, start and end of school foreach (range( 0, $datediff ) as $day) { $internal_date = date( INTERNAL_FORMAT, strtotime( "{$startDate} + {$day} days" ) ); $this_day = date( INTERNAL_FORMAT, strtotime( $internal_date ) ); $this_month = date( INTERNAL_FORMAT, strtotime( $internal_date ) ); if($haveDesdobramento === "1") { if ((isSegunda( $internal_date )) && !isExcludedDate( $internal_date )) { $cronograma[$this_month][] = $this_day; if($temposSegunda != null) { if($sum>=$horasAll){ $nhoras = $sum - $horasAll; $temposSegunda = $temposSegunda - $nhoras; echo "tSegunda: ".$temposSegunda; } if ($haveDesdobramento === "1" && $haveAllClass === null) { if($totais<=$horasTurnos){ if ($sameDayShifts === "1") { $cronograma[$this_month][] = $temposSegunda; $cronograma[$this_month][] = $temposSegunda; } else{ $days = floor($horas/$temposSegunda); $hours_remaining = $horas - $days * $temposSegunda; $cronograma[$this_month][] = $hours_remaining; } } } if ($haveDesdobramento === "1" && $haveAllClass === "1") { if($sum<=$horasAll){ if ($sameDayShifts === "1") { $cronograma[$this_month][] = $temposSegunda; $cronograma[$this_month][] = $temposSegunda; } else { $cronograma[$this_month][] = $temposSegunda; $cronograma[$this_month][] = 0; $total += $temposSegunda; $cronograma[$this_month][] = $total; } } } else{ $days = floor($horas/$temposSegunda); $hours_remaining = $horas - $days * $temposSegunda; $cronograma[$this_month][] = $hours_remaining; } } $sum += $temposSegunda; } }
It works but it stops at 102, not 100 like it should (choosing 3 hours each time) and the array returned gives me this
Any help?
Thanks
2021-06-07 - 3 - 0 - 99 2021-06-14 - 3 - 0 - 102 Notice: Undefined offset: 1 in /home/esmaior/public_html/miga/db/crud/profissionais/response.php on line 661 2021-06-21 - - 0 - 102 Notice: Undefined offset: 1 in /home/esmaior/public_html/miga/db/crud/profissionais/response.php on line 670 Notice: Undefined offset: 1 in /home/esmaior/public_html/miga/db/crud/profissionais/response.php on line 661 2021-06-28 - - 0 - 102 Notice: Undefined offset: 1 in /home/esmaior/public_html/miga/db/crud/profissionais/response.php on line 670 Total: 102
Hi I am new to PHP and I am having an issue with a foreach loop combined with an if statement, basically the if statement is getting the data from the first result in the foreach but not getting the second result, I have tried implement a count by using a variable to iterate but it isnt working: Code as follows: foreach(findCustomAlerts($customerid) as $key=>$value){ echo "ID of cat : ".$rifdid = $value['Catid']."<br>"; echo "Visits required per day : ".$visitsneeded= $value['Visits required']."<br>"; } foreach(getTodaysVisits($device_id) as $rfid){ foreach($rfid as $key=> $read){ if($key ==$rifdid && $read < $visitsneeded) { echo"<br>". $key." has not visited enough"; } } } Ouput is :
ID of cat : 5609 Array ( [rfid_id] => Array ( [23641] => 1 [5609] => 3 ) ). -------> this is the array data
----------------------------------------------- How can I get the loop to iterate to the next value?
Edited October 3, 2020 by Barand code block/indents Hey there folks, Im trying to get a foreach loop working correctly from a json source epg heres my code: https://pastebin.com/FjHj1EF6 Here is the current output, which.. while correct is only the first instance of these values i wanted using the foreach loop, i would love any tips on why my approach failed and to be pointed in the right direction. While it has to be included: $url = "https://mywebsite.fake/mlb.php?id={$arr['games'][0]["gamePk"]}"; this value isn't related to the assistance im asking for. that will work for my purposes later. but for now i just need to understand the issue with the foreach loop. Thanks in advance for your thoughtful response. mosb3rgler Let me explain my problem. I have an array with dates and numbers in format ($cronograma) Ex: Array ( [2020-09-21] => Array ( [0] => 2020-09-21 [1] => 2 [2] => 2 [3] => 2 ) [2020-09-28] => Array ( [0] => 2020-09-28 [1] => 2 [2] => 2 [3] => 4 ) Then i have another array with 2 ids (in this case 58,60) ($id) Finally i have a third array with numbers only (in this case 34,34) $tot So what i want is cross information beween them, for example for id 58 I must get dates (first element and last element when $tot = 34) for id 60 I must get dates (first element after $tot =34 and last element of array) Whath i have so far is this foreach ($id as $idPlan) { foreach ($cronograma as $c) { $t1 = 0; foreach ($tot as $d) { $t1 += (int)$d['tempos']; if ($c[3] == $t1) { $newAr[] =$idPlan; $newAr[] = $c[0]; } } } } My response array(8) { [0]=> string(2) "58" [1]=> string(10) "2021-02-01" [2]=> string(2) "58" [3]=> string(10) "2021-06-14" [4]=> string(2) "60" [5]=> string(10) "2021-02-01" [6]=> string(2) "60" [7]=> string(10) "2021-06-14" } null So it's clear that i have all repeated I should have a line like: 58 - 2020-09-21 -2021-02-01 Any help? This topic has been moved to Application Frameworks. http://www.phpfreaks.com/forums/index.php?topic=355772.0 First time posting code and very new to php aka learning as I go... The below code works as shown, but in the 'else' clause, I'd like to replace the +4 with a variable. How to do this? I've tried, Code: [Select] $renewaldate = strtotime($subscriptiondate); $sft = "' +" . $duesmultiplier . " year'"; $next_year = date('Y-m-d',strtotime($sft,$renewaldate)); but this doesn't return what I expect. Perhaps this can't be done using this technique? Code: [Select] if($n == 0) { $renewaldate = strtotime($subscriptiondate); $next_year = date('Y-m-d',strtotime('next year',$renewaldate)); echo $next_year; } else { $renewaldate = strtotime($subscriptiondate); $next_year = date('Y-m-d',strtotime('+4 year',$renewaldate)); echo $next_year; } Thank you when i use strtotime('+3 HOURS') everything is fine. so why cant i use strtotime('+3.5 HOURS')? what would be the proper way to do this? Hi, I have a bit of an issue with my application, I didn't notice until today, Sunday... Apparently PHP sees Sunday as the first day of the week, which I cannot fathom where it got hat notion from... but anyway, so I have the following samples of my code: Code: [Select] $mondayoflastweek = date('Y-m-d H:i:s', strtotime('monday last week')); $sundayoflastweek = date('Y-m-d 23:59:59', strtotime('sunday last week')); The crazy and very troublesome thing about the results of that code is that if you run that today, Sunday, it shows today as last week... So therefore, the next bit of code is just as problematic: Code: [Select] $mondayofthisweek = date('Y-m-d H:i:s', strtotime('monday this week')); $sundayofthisweek = date('Y-m-d 23:59:59', strtotime('sunday this week')); As you can imagine, for payroll, this is causing a big problem... My week starts at precisely 12:00 am on Monday and ends at precisely 11:59 pm on Sunday... Could someone please help me out with this... Hello,
I'm trying to use strtotime to add time to a mysql type timestamp. Any help would be appreciated. I think I have tunnel vision from looking at it when I know it has to be something obvious...lol
$start = "2014-05-22 09:16:24"; $type = 30; $endtime = date($start,strtotime("+ '.$type.' minutes")); echo "End: ".$endtime."</br>Start: ".$start;Right now it returns: End: 2014-05-22 09:16:24 Start: 2014-05-22 09:16:24 I'm trying to add 5 days to $today date: Code: [Select] <?php $exp_date = "2011-09-11"; $todays_date = date("Y-m-d"); $today = strtotime("+5 days", $todays_date); $expiration_date = strtotime($exp_date); if ($expiration_date > $today) { echo "Valid: Yes"; } else { echo "Valid: No"; } ?> Is +5 days used wrong? So, my problem is that I need to edit my Xmas calculator to understand when this year's Xmas is over, it will automatically jump to the next one (2011-12-25). I have no idea how to do this (noob to php...) Thanks in advance. Here's my code: Code: [Select] <?php $time=time(); $xmas=strtotime("2010-12-25 00:00:00"); $diff = $xmas - $time; $days=intval($diff/86400); $left=$diff%86400; $hs=intval($left/3600); $left=$left%3600; $mins=intval($left/60); $secs=$left%60; echo "<font size=12 face=corbel> Xmas is after:<br> <strong>$days days</strong><br> <strong>$hs hours</strong><br> <strong>$mins mins $secs secs</strong>! </font>"; ?> Hello! A user is going to input a date in the form "mm/dd/yyyy". I'd like to convert this to a time stamp so that I can store it via MySql. I've heard about the "strtotime" function in PHP but I'm not sure how it can tell that the user 03/04/2011 represents March,4th 2011 and not, say April,3rd 2011 (European version). Help with the correct syntax for conversion would be greatly appreciated. Thank you, Eric I have this php code: length is 15 , that is what it is coming out of the database $length = $get['length']; $time = date('Y-m-d', strtotime('+$length days')); But when I echo time it comes out with this result 1969-12-31 What am I doing wrong? what is the propper way to write Code: [Select] $payrollweek="10"; date('Y-m-d', strtotime("+($payrollweek - 2) week $payrollend") ), "<br />"; its the ("+($payrollweek - 2) that isnt working i think can someone please tell me how to echo the current time or date? I have the date in the format 1306768978 in the database but I dont want to echo it back that way obviously. Not only that but does anybody know of a good tutorial on how to use this function? Hey freaks, running into a little issue with the use of date and strtotime.... basically I wanna present the date in the format m-d-y to the user and insert it into the DB in the Y-m-d format. What seems to be happening is a freaky Y-d-m format and I don't know why this is. This code is an example that expresses my frustration. Code: [Select] $nmon = strtotime("next Monday"); $next = date('m-d-Y', $nmon); $_SESSION['REPORT_DUE'] = $next; $nfri = strtotime("Friday"); $due = date('m-d-Y', $nfri); $_SESSION['WEEK_ENDING'] = $due; $ses1 = $_SESSION['WEEK_ENDING2'] = date('Y-m-d', strtotime($due)); $ses2 = $_SESSION['WEEK_ENDING3'] = date('d-m-Y', strtotime($ses1)); .....output.... Quote [WEEK_ENDING] => 09-02-2011 [WEEK_ENDING2] => 2011-02-09 [WEEK_ENDING3] => 09-02-2011 ) Thank you for any assistance. Little confuzzled. I have got a bunch of listings that will be printed at the end of the page but I need to subtract a time specified in a textbox (send it to itself) on the page. Code: [Select] $row['active'] = (strtotime('$row[etd]') + $rwy1 = $_POST['tmatttextfield']);Well this just converts the time in the database which is always going to be a four character number such as: 1300 for 1PM into a time and adds the textbox value to the time. Code: [Select] <td width="50" bgcolor="<?php echo $bc?>"><strong><?php echo date('hi', $row['active']);?></strong></td>That just prints it all out formatting the time to only Hours and Minutes. So just to clarify the user would put in say '10' in the textbox which would constitute 10 minutes. The code above should then define that '10' as 10 minutes. Define the database result, ie. 1500 as 3PM and add the 10 minutes to it so it would become 1510. I have been getting some wierd and wonderful results whilst trying to get it to work. Mainly just 1200 though. Harry. how do I use strtotime() instead of mktime()? $matchTimea = mktime($mHour, $mMinute, 0, $mMonth, $mDay, $mYear); if($matchTimea-time() < $cutoffTimea*60) { |