PHP7 non-numeric value encountered - php

I'm converting dates (Gregorian to Islamic Hijri). PHP 7 is displaying a "non-numeric" warning error. How can I amend the code?
This is for a Linux server running PHP 7.2 and Apache. It worked as expected with PHP 5! Have tried suggestions on stackoverflow, e.g. (intval) + (int), to no avail!
1510 $date = "25/1/1999";
1511 $Gdate = explode ('/', $date) ;
1512 $day = $Gdate[0];
1513 $month = $Gdate[1];
1514 $year = $Gdate[2];
1515
1516 $jgc=0;
1517 $m=$month;
1518 $y=$year;
1519 if($m<3)
1520 {
1521 $m=$m+12;
1522 $y=$y-1;
1523 }
1524 $c=floor($y/100.);
1525
1526 if($y==1582 && $m>10) {$jgc=10;}
1527 if($y>1582) {$jgc=2-$c+floor($c/4.);}
1528
1529 $jd= floor(365.25*($y+4716))+floor(30.6001*($m+1))+$day+$jgc-1524;
I expect it to simply work and not produce the following error:
Warning: A non-numeric value encountered in process.php on line 1529

Trying this code on php 7.2.4 works without error so I think its something on your specific php.
Try to convert to integer your variables before make calculatings
$date = "25/1/1999";
$Gdate = explode ('/', $date) ;
$day = (int)$Gdate[0];
$month = (int)$Gdate[1];
$year = (int)$Gdate[2];
$jgc=0;
$m=$month;
$y=$year;
if($m<3)
{
$m=$m+12;
$y=$y-1;
}
$c=floor($y/100.);
if($y==1582 && $m>10) {$jgc=10;}
if($y>1582) {$jgc=2-$c+floor($c/4.);}
$jd= floor(365.25*($y+4716))+floor(30.6001*($m+1))+$day+$jgc-1524;
echo $jd;

The problem says, you have a non numeric value.Then, I think your date is malformed. I suggest you use DateTime() library for correct parsing dates.

Related

Fixing "Warning: Illegal string offset" -- (but without losing content)

I have searched for solution for this problem but none fix my problem.
The answers suggest that I use isset to check the array before working on it. But I will explain how it doesnt do it for me later.
Pre-req:
I've got a huge XML file from a tour & travel webservice which I would parse and convert to PHP array and later do some operation on it. (Filter tours mostly).
My Approach:
I'm using SimpleXML to load the xml and convert it to PHP array like so:
$xml = file_get_contents(APPPATH."tour.xml", true);
$xmlString = htmlentity_to_xml($xml); //custom method to clean XML
$Str = simplexml_load_string($xmlString, 'SimpleXMLElement', LIBXML_NOCDATA);
//converting to array
$json = json_encode($Str);
$array = json_decode($json,TRUE);
Then I'm sending this array to a fitlerTours($searchParams, $tourArray) method along with search parameters (cityName & dates) and the array itself.
Then using foreach() i'm going through each tour to look for the cityName and raising a flag if found.
The Problem
When I filter the tours (the ones which contain cityName) for dates, I'm getting this.
Severity: Warning
Message: Illegal string offset 'year'
Filename: controllers/tourFilter.php
Line Number: 78
Warning shows for offeset 'month' and 'day' also.
Here's my PHP for date filter: (Line 78 is 4th line)
if($flag == 1){
if(!empty($fromDate)){
foreach($tour['departureDates']['date'] AS $date){
$dateDep = strtotime($date['year'] . "-" . (($date['month']) < 10 ? "0".$date['month'] : $date['month']) . "-" . (($date['day']) < 10 ? "0".$date['day'] : $date['day']));
if(strtotime($fromDate) <= $dateDep && $dateDep <= strtotime($fromDate . "+".$range." days")){
if($date['departureStatus'] != "SoldOut"){
$dateFlag = 1;
}
}
}
}
else{
$dateFlag = 1;
}
$flag = 0;
}
if($dateFlag == 1){//Collect tours which contain the keyword & dates to $response array
$responseArray[] = $tour;
$dateFlag = false; //Reset Flag
}
Here's the snippet of XML:
...
<departureDates>
<date>
<day>7</day>
<month>1</month>
<year>2016</year>
<singlesPrice>12761</singlesPrice>
<doublesPrice>9990</doublesPrice>
<triplesPrice>0</triplesPrice>
<quadsPrice>0</quadsPrice>
<shipName/>
<departureStatus>Available</departureStatus>
</date>
<date>
<day>8</day>
<month>1</month>
<year>2016</year>
<singlesPrice>12761</singlesPrice>
<doublesPrice>9990</doublesPrice>
<triplesPrice>0</triplesPrice>
<quadsPrice>0</quadsPrice>
<shipName/>
<departureStatus>SoldOut</departureStatus>
</date>
</departureDates>
...
Now if i use the solution I found by searching around is check if the array is set properly by isset() it doesn't return true and line 78 is not executed and the data is lost. But I need the data.
This happens for only keywords i search.
Any help is appreciated.
The error says that the $date var is detected as string in some point...
Characters within strings may be accessed and modified by specifying
the zero-based offset of the desired character after the string using
square array brackets, as in $str[42]. Think of a string as an array
of characters for this purpose.
See here
So try this:
if(is_array($date)){
$dateDep = strtotime($date['year'] . "-" . (($date['month']) < 10 ? "0".$date['month'] : $date['month']) . "-" . (($date['day']) < 10 ? "0".$date['day'] : $date['day']));
if(strtotime($fromDate) <= $dateDep && $dateDep <= strtotime($fromDate . "+".$range." days")){
if($date['departureStatus'] != "SoldOut"){
$dateFlag = 1;
}
}
}
else {
//If this is not an array what is it then?
var_dump($date);
}

php explode microtime error: Undefined offset: 1

I have this code which is showing this error in the logs. Any ideas why?
PHP Notice: Undefined offset: 1
$microtime = microtime(true);
list($time,$mili) = explode(".", $microtime);
EDIT:
Sorry forgot to mention that this error doesn't happen every time, I just noticed it in the logs. Maybe it only happens when there are no milliseconds
Example output of microtime: 1418114280.8363
A bit too long for a comment, but (as an alternative) try using
$microtime = microtime(true);
$time = floor($microtime);
$mili = fmod($microtime, 1);
and see what result this gives
It works fine check it
echo $microtime = microtime(true);
print_r(explode(".", $microtime));
list($time,$mili) = explode(".", $microtime);
print_r($time);
echo '---';
print_r($mili);

Why the error message in php split() function? [duplicate]

This question already has answers here:
Deprecated: Function split() is deprecated. How to rewrite this statement?
(4 answers)
Closed 9 years ago.
My task is to split a date fetching from DB and find the date after 8 years.
My tries are here -
Variables:
$doo = $info['s_doo']; // 2013-05-01
$validity = $info['s_validity']; // 8
Try 1
$str="+".$validity." year";
echo date("d / m / Y",strtotime($str,$doo)); // Does not work
Try 2
$str="+".($validity*12)." month";
echo date("d / m / Y",strtotime($str,$doo)); // Does not work
Try 3
$str="+".($validity*52)." week";
echo date("d / m / Y",strtotime($str,$doo)); // Works but Wrong result
Finally
list($y, $m, $d) = split('-',$doo); // Line 107
$str = ($y+$validity)."-".$m."-".$d;
echo date("d / m / Y",strtotime($str)); // 01 / 05 / 2021
The output stands:
Deprecated: Function split() is deprecated in D:\****\accinfo.php on line
107 01 / 05 / 2021
If it's generating a correct output why the error message is being displayed? I don't know what the Deprecated message for.
I also tried using array instead of list and the split function like - split('-',$doo,10); split('-',$info['s_doo'],10); split('[-]',$doo); etc...
I need a good way to do the task. Thanks you.
Use DateTime instead:
// input date (Y-m-d ?)
$doo = '2013-05-01';
// 8 years ?
$validity = new \DateInterval('P8Y');
// convert input date to DateTime object and add validity
$doo = \DateTime::createFromFormat('Y-m-d', $doo);
$doo->add($validity);
print $doo->format('d/m/Y');
Deprecated means that PHP language is going to stop support for the function in future. It will be removed from up coming versions of the language and so if you have a working code now, and you upgrade your PHP in the future your code will break because it is not available in this new version. Every deprecated function gets replaced by a new better function. Find that one and replace your function with the new one.
In order to inform users, PHP will show the deprecated message even if the function in question works currently in the present PHP version.
As stated HERE split() is deprecated.
Use explode() instead like this:
list($y, $m, $d) = explode( '-' , $doo );
split() function is deprecated. You should use explode('-',$doo) which will split the string into an array.

php date() function not functioning when fed with data retrieved from xml

I've a php file in which I've codes like
$xml_time = $update->$node->timestamp; **//Case 1**
$time = date("c",$xml_time);
$normal_time = time(); **//Case 2**
$time = date("c",$normal_time );
The variable $xml_time is retrieved from an external xml file using simpleXML. The time is stored using the time() function at some earlier point.
The problem is that, when I call the line $time = date("c",$xml_time); (is Case 1), I get an error message saying <b>Warning</b>: date() expects parameter 2 to be long, object given in <b>C:\xampp\blah\blah\blah\ajax.php</b> on line <b>46</b><br /> but in Case 2, no error shows up.
Can anyone help me identify the problem??
try if this works:
$xml_time = (integer) $update->$node->timestamp; **//Case 1**
$time = date("c",$xml_time);
This will typecast SimpleXML object to integer.

Parsing date in a Facebook application using PHP

i'm trying a simple facebook application, running on localhost using wamp 2.0 , i got the date using the following code as shown here, when i run echo($bd); i get the correct result but when the following code runs i get an error.
Code
$bd = $fbme['birthday'];
$datearr = "0";$month = "0"; $date = "0"; $year = "0";
$datearr = explode('-', $bd);
list($month,$date,$year) = $datearr;
echo($month);
Error :
Notice: Undefined offset: 2 in C:\wamp......\index.php on line 30
Notice: Undefined offset: 1 in C:\wamp.....\index.php on line 30
Could you please suggest the reason and why this is occuring, any way how to get rid of this. Thanks a lot!!
The string stored in $fbme['birthday'] does not contain any '-' chars. Most likely it is empty. Check where $fbme is populated and an actual birthday is present.

Categories