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.
Related
I'm stuck with this, and till now I didn't catch where I'm making mistake. My code in functions page is:
<?PHP
function getDate()
{
$found= 2014;
$curent= date('Y');
echo $found . (($curent!= $found) ? '-' . $curent: '');
}
?>
and on index page, I called that function with next line of code <?php echo getDate(); ?> Error i get is: HTTP ERROR 500
If you turn on error reporting or consult your server's logs (this is a critical part of developing in PHP), you'll see the following:
Fatal error: Cannot redeclare getDate() ... on line 7
That's because getdate is already a built-in PHP function.
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.
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))
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
trying to get every post and comment from a facebook page, I made this function that should go through the pagination:
$req = $facebook->api("/" . $pagename . "/?fields=posts.fields(message,link,created_time,shares,likes,comments)");
function parcours_arbre($ab)
{
GLOBAL $facebook;
GLOBAL $pagename;
$next = create_request($ab['posts']['paging']['next']);
$next_req = $facebook->api($pagename.$next);
$ab_next = $next_req['data'];
$prev = create_request($ab['posts']['paging']['previous']);
$prev_req = $facebook->api($prev);
$ab_prev = $prev_req['data'];
if (empty($ab)) {
display_results($ab['posts']['data']);
} else {
parcours_arbre($ab_next);
parcours_arbre($ab_prev);
}
}
I unfortunately get the following error:
Notice: Undefined index: posts in /form.php on line 36
Notice: Undefined offset: 3 in /utils.php on line 20
Notice: Undefined offset: 4 in /utils.php on line 20
Fatal error: Uncaught GraphMethodException: Unsupported get request. thrown in /sdk/src/base_facebook.php on line 1271
Any idea how i could avoid it or what is going on? Would this go away if i use the "until" statement in my api request?
Thanks a lot,
To explain each error
the variable $ab which is an argument to the function, does not have a "posts" index. You should try to var_dump this variable so you can see what it actually looks like.
same as above
same as above
the api function takes 3. The path which should just be #pagename. The method ("GET" or "POST") most likely POST because GET is causing an error. The parameters, which should be array("fields" => "posts.fields(message,link,created_time,shares,likes,comments)")
I noticed that for next you have the code
$next_req = $facebook->api($pagename.$next);
but for previous you have
$prev_req = $facebook->api($prev);
Might want to look into this.