PHP - DateTime->add not working. Says DateInterval format is wrong - php

I've been struggling for a few hours on an error that I'm sure it's silly, but I can't find to solve it.
My idea is to create a DateTime of (00:00:00), in order to add to it in a loop 4:30 hours per $i.
$saldo_funcionario = new DateTime('00:00:00');
for ($i=0; $i<$value; $i++) {
$tempo_dia_funcionario = new DateInterval('PT4H30I');
$saldo_funcionario->add($tempo_dia_funcionario);
}
The error message is:
Fatal error: Uncaught exception 'Exception' with message 'DateInterval::__construct(): Unknown or bad format (PT4H30I)'
DateInterval->__construct('PT4H30I') #1 {main} on line 75
Where am I missing?

$tempo_dia_funcionario = new DateInterval('PT4H30I');
Should be:
$tempo_dia_funcionario = new DateInterval('PT4H30M');
Notice 'M' instead of 'I'.
http://php.net/manual/en/dateinterval.construct.php

Related

PHP date function causes problems if input value is empty

I am using the following PHP code to get the upcoming date of a given day.
For example If the day given in "Tuesday" it will output 01-09-2020.
The function is to show upcoming sale dates of smartphones.
$saledate = "Tuesday"
date_default_timezone_set('Asia/Kolkata');
function dayDate($day) {
return new DateTime('next ' . $day);
}
$nextsale = dayDate($saledate);
if(!empty($saledate)) {
echo "Next Flash Sale on $saledate ";
echo $nextsale->format('d-m-Y'); // 01-09-2020
}
The problem is if the $saledate is empty, it causes the entire web page to not load. What could be the issue ?
This is what the errorlog says
[25-Aug-2020 18:47:15 Asia/Kolkata] PHP Fatal error: Uncaught Error: Call to undefined function dayDate() in /home/customer/www/sitename.com/public_html/index.php:1114
Stack trace:
#0 {main}
thrown in /home/customer/www/sitename.com/public_html/index.php on line 1114
Update: Fixed by adding the $nextsale = dayDate($saledate); inside the if block.

Date POST switching types

I don't know really how to elaborate on this problem. I have the following markup in my form:
<input class="form-control" type="date" name="renewal" required="required" id="renewal" />
and if I call var_dump it tells me that it is a String value of the date formatted in a Y-m-d format. However, if I attempt to convert the String to a DateTime object using the following:
$_POST['renewal'] = new DateTime($_POST['renewal']);
I get the following error message:
Fatal error: Uncaught TypeError: DateTime::__construct() expects parameter 1 to be string, object given in *.php:212 Stack trace: #0 *.php(212): DateTime->__construct(Object(DateTime)) #1 {main} thrown in *.php on line 212
So if I try to explicitly convert $_POST['renewal'] to a String using strval:
$_POST['renewal'] = new DateTime(strval($_POST['renewal']));
I receive the following error:
Recoverable fatal error: Object of class DateTime could not be converted to string in *.php on line 212
And if I try to get the format from $_POST['renewal'] using:
$_POST['renewal'] = new DateTime($_POST['renewal']->format('Y-m-d'));
I receive the following error:
Fatal error: Uncaught Error: Call to a member function format() on string in *.php:212 Stack trace: #0 {main} thrown in *.php on line 212
And if I try to not do anything to the POST value and try to set the date's year to this year's value using the following:
$_POST['renewal'] = new DateTime($today->format('Y') . $_POST['renewal']->format('-m-d'));
I receive the same call to a member function error as shown above. So why does it tell me when I attempt to convert the POST value to a DateTime object that it expects a String and not a DateTime object, but whenever I try to use the value as a DateTime object it says that the object is a String?
UPDATE
To show you what I'm trying to do, I am trying to see if the value of $_POST['renewal'] is greater than today's date. If the value of the POST greater than today's date then I'm changing the value to today's year and if the value is still not greater than today's date then I'm also adding 6 month's to the date. Here is the code that I'm trying to use:
$today = new DateTime("now");
$_POST['renewal'] = new DateTime($_POST['renewal']->format('Y-m-d'));
if ($_POST['renewal'] < $today) {
$_POST['renewal'] = new DateTime($today->format('Y') . $_POST['renewal']->format('-m-d'));
if ($_POST['renewal'] < $today) {
date_add($_POST['renewal'], date_interval_create_from_date_string('6 months'));
}
}
The value that I'm using to test the code is 2016-06-28, and what I expect it to do is change to 2018-06-28 and then again to 2018-12-28. But if I send the value of 2017-11-01 then it would change to 2018-11-01 and if I send the value of 2018-12-01 then it would not change at all.

PHP Uncaught exception when trying to get the first row of an Excel table

I have a Excel table and I'm using PHPExcel to read and display it. And now I want to get the first row. So here's my code:
$column = ord($sheet->getHighestColumn())-64;
for($i = 0; i<$column; $i++){
echo $sheet->getCellByColumnAndRow($i,1) . ' ';
}
And this is what I get:
country_code country_name Nom du pays modification_flag tag Code ISO Alphabétique 2 positions Iban O/N
So I have 7 columns and the output is correct but I also have an fatal error
Fatal error: Uncaught exception 'PHPExcel_Exception' with message 'Invalid cell coordinate [AA1' in /Library/WebServer/Documents/BICIBAN/PHPExcel-1.8/Classes/PHPExcel/Cell.php:594 Stack trace: #0 /Library/WebServer/Documents/BICIBAN/PHPExcel-1.8/Classes/PHPExcel/Worksheet.php(1218): PHPExcel_Cell::coordinateFromString('[AA1') #1 /Library/WebServer/Documents/BICIBAN/PHPExcel-1.8/Classes/PHPExcel/Worksheet.php(1200): PHPExcel_Worksheet->createNewCell('[AA1') #2 /Library/WebServer/Documents/BICIBAN/lecture.php(112): PHPExcel_Worksheet->getCellByColumnAndRow(18278, 1) #3 {main} thrown in /Library/WebServer/Documents/BICIBAN/PHPExcel-1.8/Classes/PHPExcel/Cell.php on line 594
I may know what it is trying to say, when I have many columns and I have for instance the AA column. So I did a function that can convert until the ZZ column but that does not correct my fatal error.
Thanks for your help
Oh, I just found what is wrong... I just forgot a $ in my for loop
for($i = 0; $i<$column; $i++){
FeelsBadMan :(

I am getting a Fatal error when trying to go to a page

I am getting a Fatal error when trying to go to a page
Fatal error: Can't use function return value in write context in
/home/site/public_html/welcome/oc/classes/controller/panel/order.php
on line 49
I looked into the code and found this
//filter date
if (!empty(Core::request('from_date')) AND (!empty(Core::request('to_date')))
{
//Getting the dates range
$from_date = Core::request('from_date',strtotime('-1 month'));
$to_date = Core::request('to_date',time());
$orders = $orders->where('pay_date','between',array($from_date,$to_date));
}
can you tell me if there is something wrong with this code? thanks
When i remove the page the page works so i think it have something to do with the code or maybe it can't get the information from the database
It is because Core::request() has return statement and you cant you it in write context. Try with -
$fromDate = Core::request('from_date');
$toDate = Core::request('to_date');
if (!empty($fromDate) AND (!empty($toDate))

How can I fix this PHP XML parsing error?

This is my first question :).
Im writing a little twitter app in PHP that sends a DMs to all your followers. What im trying to do right now is to get the list of followers. So through twitter api and getting all usernames but for some reason this parsing error appear. Im new to php(but not so much to programming), I actually started learning it yesterday so please be easy on me ;).
Here is the code:
$t= new twitter();
$t->username= $_GET["username"];
$t->password= $_GET["password"];
$fi = $t->followers();
$xml[$page] = new SimpleXMLElement($fi[2]);
$user1count=0;
while(isset($xml[$page]->user[0])){
foreach ($xml[$page]->user as $user) {
$userdet[(string)$user->screen_name]=array( ’screen_name’=> (string)$user->screen_name, ‘location’=>(string)$user->location, ‘description’=>(string)$user-> description, ‘profile_image_url’=> (string)$user-> profile_image_url, ‘url’=>(string)$user-> url, ‘name’=>(string)$user->name );
$user1details[$user1count]= (string)$user->screen_name;
$user1count++;
}
$page++;
$fi=getfilecontents($friendsurl.$username1."xml?page".$page);
if($fi[0]===false){
echo ("Error :".$fi[1]);
$err=new SimpleXMLElement($fi[2]);
echo " ".$err->error." ";
// echo ““;
die();
}
$xml[$page] = new SimpleXMLElement($fi[2]);
}
And the error said:
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /Applications/XAMPP/xamppfiles/htdocs/scripts/dmsend.php:125 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/scripts/dmsend.php(125): SimpleXMLElement->__construct('') #1 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/scripts/dmsend.php on line 125
Thank you! :)
It looks like $fi[2] is not a valid xml string. I am not 100% familiar with the twitter API, but I would do a var_dump($fi) and evaluate what is begin returned. From there, you should be able to figure out what is happening.

Categories