Php, date_modify() with a variable - php

I need to use the indexed element as the amount to change the date by
$id = $_POST['id'];
$ids = explode(',',$id);
$dateCreated = date('Y-m-d');
$dateExpiredtemp =date_create($dateCreated);
date_modify($dateExpiredtemp,'+ $ids[2]');
$dateExpired = date('$dateExpiredtemp');
date_modify($dateExpiredtemp,'+ $ids[2]');
This line provides this error
Warning: date_modify(): Failed to parse time string (+ $ids[2]) at position 0 (+): Unexpected character in
$ids[2] is a string and it needs to be carried into the altering parameter of date_modify

Change this line of code
date_modify($dateExpiredtemp,'+ $ids[2]');
into
date_modify($dateExpiredtemp,'+ ' . $ids[2]);
Your initial code will not recognize the $ids[2] since it is included in the string. Therefore, $ids[2] won't be parsed.

There seems to be something wrong with your $ids array.
I assume that $ids[2] is just a number and that is why you're getting an error. You need to specify a unit of time such as days. Take a look at the code below, I have added days to the end of the 2nd parameter in the date_modify function.
$id = $_POST['id'];
$ids = explode(',',$id);
$dateCreated = date('Y-m-d');
$dateExpiredtemp =date_create($dateCreated);
date_modify($dateExpiredtemp,'+ $ids[2] days');
$dateExpired = date('$dateExpiredtemp');

Related

How to use the result of mysql query as the column name of another query

i have a following code
<?php
include("includes\conn.php");
$start = $_POST['start_date'];
$end = $_POST['end_date'];
$result = mysqli_query($con,"SELECT * FROM data");
while($row = mysqli_fetch_assoc($result)){
$time_stamp = $row['time_stamp'];
$temp_date= substr($time_stamp, 2, 12);
$date= explode('.', $temp_date);
$final= $date[2]. '-' .$date[1]. '-' .$date[0];
$result1 = mysqli_query($con,"SELECT * FROM data WHERE $final BETWEEN $start AND $end");
while($row2 = mysqli_fetch_assoc($result1)){
print_r($row2);
}
}
?>
Error:
Notice: Undefined offset: 2 in C:\xampp\htdocs\Nestle\trend_test.php on line 10
Notice: Undefined offset: 1 in C:\xampp\htdocs\Nestle\trend_test.php on line 10
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given in C:\xampp\htdocs\Nestle\trend_test.php on line 12
Your kind help is needed!
Your date extraction from time_stamp might be a problem.
$final= substr($time_stamp, 0, 10)
would be better.
If needed include Quotation marks at start and end of date items within query.
Add another echo for $final before calling MySQL and you can understand better and correct yourself.
Lets assume that this is the data of the date in your db '09.12.2019 12:54:50' so in order to get the format yyyy-mm-dd, hence your date format has date and time, first is you remove the time:
$temp_date= substr('09.12.2019 12:54:50', 0, 10);
After getting the date, we will be making it to this format yyyy-mm-dd
$date= explode('.', $temp_date);
$final= $date[2]. '-' .$date[1]. '-' .$date[0];
As a result, $final has a value 2019-12-09 [yyyy-mm-dd] and you can now use it in the next query.
Since your date values in db uses '.' (09.12.2019 12:54:50) instead of
'-' (09-12-2019 12:54:50), we made an approachable solution in it.

Highchart add three zeros in the loop

I have use library chart from this page link. Unfortunately, the data I download is not compatible, for example:
I get from JSON time:
time: 1346803200
this time is not displayed on the chart. I must add three zeros at the end (look like this: 1346803200000), then the chart displays correctly. So I have code:
for ($i=0; $i < count($chart['Data']) ; $i++) {
$time = $chart['Data'][$i]['time'];
}
I need add to variable $time numeric 000 (three zeros at the end). I can not add it this way:
$time = $chart['Data'][$i]['time']."000";
because variable $time change from int to string. I must have $time in integer type. Is there any way to add three zeros without changing the variable type inside the loop ?
Not sure why you are doing this or if there is a better way, but if type conversion is the only thing that worries you, you can explicitly cast it to int:
$time = (int)($chart['Data'][$i]['time']."000");
Also, not sure if this is your desired behavior, but just note that your $time variable will get overwritten with every iteration of the for loop.
And one more thing, you can achieve your desired output without the explicit conversion by just multiplying your result with 1000, like so:
$time = $chart['Data'][$i]['time'] * 1000;
This should be a better solution than concatenation when you are working with ints
Seriously?
$time = $chart['Data'][$i]['time'] * 1000;
You con multiply for 1000
$time = $chart['Data'][$i]['time']*1000;

Same variable type (String) but not work when use

10-01-2014 ===> means 10 jan 2014
i have source code like this:
foreach($report_data['summary'] as $key=>$row) {
$substrdate=substr($row['payment_type'],-16); //i have check the result is 10-01-2014 & 11-01-2014
$stringvar = '10-01-2014';
$date = DateTime::createFromFormat('d-m-Y', $stringvar);
$summary_data_row[] = array('data'=>'<span style="color:'.$color.'">'.$date->format('Y-m-d').'</span>', 'align'=>'right');
$summary_data_row[] = array('data'=>'<span style="color:'.$color.'">'.$row['comment'].'</span>', 'align'=>'right');
}//end of foreach
it runs well '10-01-2014' become '2014-01-10' in string type as what i want ..until i substitute variable $stringvar with $substrdate which has same value -> '10-01-2014' , syntax $date->format('Y-m-d') makes my program blank page. no error shown in logs.
pls help
I also faced the same problem. and my solution was the following code.
$stringvar = '12-01-2014';
$date= date('Y-m-d',strtotime(str_replace("-","/",$stringvar)));
Try this
//$stringvar = '10-01-2014';
//$date = DateTime::createFromFormat('d-m-Y', $stringvar);
Use this instead of above one
$stringvar = '10-01-2014';
$date = date("Y-m-d", strtotime($stringvar));

PHP is concatenating instead of adding

I'm having this problem where my PHP code is concatenating instead of adding
$offset=$_POST['offset']; //Get the offset
$searchLimit = 10;
$searchCount = count(sql) //For the purpose of this question, it returns the result count
Now I want to calculate the 'from' display for pagination, so I do
$from = ($offset*$searchLimit)+1;
It works fine when
$offset == 0
I get the expected result which is 1. But when
$offset == 1
It gives me 101. Basically it is concatenating the 10 and 1 to give me 101. I've tried the following
$from = (int)($offset*$searchLimit)+1
$from = ((int)($offset)*$searchLimit)+1
$from = (((int)($offset)*$searchLimit)+1)
I even tried
$offset = (int)$_POST['offset'];
But all of them are giving the same result.
You are missing a $ before searchLimit. As a result, it is being treated as a string. This result in unexpected behaviour.
You missed a $ sign before searchLimit (and perhaps before sql). -_-

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.

Categories