I am trying to generate date (after 7 months from now)
here is my code
$cdate = new DateTime("+7 months");
$cdate->modify("-" . ($cdate->format('j')-1) . " days");
$expiry_date= $cdate->format('Y-m-d');
$expiry_date = strtotime($expiry_date);
which gives the error:
PHP Catchable fatal error:
Object of class DateTime could not be converted to string
it is working with be before ... what is the problem??
DateTime class has no magic method __toString(), so you cannot use this object as string.
You should use getTimestamp()
$cdate = new DateTime("+7 months");
$expiry_date = $cdate->getTimestamp();
$cdate = new DateTime(+7 months);
$cdate = $cdate->format('Y-m-d');
Will cause cdate to be a string if that's what you are going for.
Related
What am I doing wrong here?
PHP Fatal error: Call to a member function add() on a non-object in
$rndM = rand(48,51);
$rndS = rand(1,59);
$ts = new DateTime($krow['GLSTRT']);
$tsd = $ts->format('Y-m-d H:i:s');
$tsup = 'PT'.$rndM.'i'.$rndS.'s';
$lamDate= $tsd->add(new DateInterval('\''.$tsup.'\''));
$krow['GLSTRT'] is a data value from MSSQL.
At first I was getting format errors with the add(new DateInterval).
I added in the format line, which resolved the format error and now the add(new DateInterval) is throwing the error at the beginning of this post.
A few things:
format() returns a string, so you cannot use add() on the result as it is not an object. You need $ts->add() instead.
You should check the right syntax for the DateInterval constructor; at the very least you need to get rid of the quotes around your string.
When you use the add() method, you modify your object, so assigning it to another variable is pointless unless you clone it first. Now both $ts (see my first point) and $lamDate will reference the same object.
The DateTime->format() has the following method signature:
public string format ( string $format ). It returns a string.
You don't need to reassign the DataTime object to work with it.
I've modified the order of the code and the variable names a little bit to make things clear:
// create new datetime object and return formatted date
$date = new DateTime($krow['GLSTRT']);
echo $date->format('Y-m-d H:i:s') . "\n";
// define interval string and create a new DateInterval object
$rndM = rand(48,51);
$rndS = rand(1,59);
$intervalString = 'PT' . $rndM . 'i' . $rndS . 's';
$intervalObject = new DateInterval($intervalString);
// add interval to date object
$date ->add($intervalObject);
// return the modified value formatted
echo $date->format('Y-m-d H:i:s') . "\n";
:banghead: Thank you guys. This is what I get for rushing a fix. Here's the final product.
$ts = new DateTime($krow['GLSTRT']);
$intervalString = 'PT' . $rndM . 'M' . $rndS . 'S';
$ts ->add(new DateInterval($intervalString));
$lamDate = $ts->format('Y-m-d H:i:s');
I have the following query in php/mysql:
$q2 = $conn3->prepare("SELECT (t.start_time + INTERVAL t.text_duration SECOND) as end_time FROM texts t WHERE t.start_time <= :starttime AND (t.start_time + INTERVAL t.text_duration SECOND) >= :starttime AND t.plot_id = :plot_id LIMIT 1");
$q2->bindValue(':starttime', $start_timestamp);
$q2->bindValue(':plot_id', $plot_id);
$q2->execute();
$check2 = $q2->fetch(PDO::FETCH_ASSOC);
$start_time is a datetime object defined as follows:
$date = new DateTime();
$start_timestamp = $date->add(DateInterval::createFromDateString('10 minutes'));
When I run it, I get the following error:
Catchable fatal error: Object of class DateTime could not be converted to string in ...
How can I fix it?
You only have a DateTime object when you called DateTime::add(). You still have to convert it to a timestamp. Use DateTime::getTimestamp() to do that. You still need to convert it to a datetime value. Use DateTime::format() to do this.
$start_timestamp = $date->add(DateInterval::createFromDateString('10 minutes'))->format('Y-m-d H:i:s');
i've tried using date_diff and date_create to get a difference from two date that's already converted to string.
here's the code:
$date_1 = date_create();
$date_now = date_format($date_1, 'Y-m-d');
//echo $date_now . "\n";
$date=date_create($date_now);
date_add($date,date_interval_create_from_date_string("3 days"));
$date_return = date_format($date,"Y-m-d");
$diff = date_diff(date_create($date_now), date_create($date_return));
echo $diff;
and i am getting this error:
Object of class DateInterval could not be converted to string
You need to call DateInterval::format() to display that difference as a string.
echo $diff->format('%d days');
See the manual for all of the available formatting options.
Using Carbon(A simple PHP API extension for DateTime) could be something like this
$date_now->diffInDays($date_return);
hope this helps. But to get more information about Carbon follow this link Carbon Docs
This problem might be common but it seems like I can't find out what I did wrong.
I have two variables the date and time from database which I think is in UTC format and I need to convert to AEST;
$date = message['date']; //25/09/2014
$time = message['time']; //9:00:00
$combined_time = $date . " " . $time;
$schedule_date = DateTime::createFromFormat("Y-m-d H:i:s",$combined_time ,new DateTimeZone("Australia/Melbourne"));
return $schedule_date->format("jS-A-Y H:i:s T");
I got Call to a member function format() on a non-object exception.
You should use ("d/m/Y h:i:s"):
$schedule_date = DateTime::createFromFormat("d/m/Y h:i:s", $combined_time , new DateTimeZone("Australia/Melbourne"));
The given format is wrong because your date-time is as 25/09/2014 9:00:00. Check this example.
You are not creating any object of class so giving you error you need to create class object or use directly.
$schedule_date = new DateTime($date);
return $schedule_date->format("jS-A-Y H:i:s T");
or you need to pass correct format in Datetime() see #The Alpha answer for this
For more read manual :- http://php.net/manual/en/datetime.createfromformat.php
// convert string to timestamp
$timeStamp = strtotime($combined_time);
// create DateTime object use timestamp
$date = new DateTime();
$date->setTimestamp($timeStamp);
Then you can use $date->format('Y') to get year...
I am trying to generate date (after 7 months from now)
here is my code
$cdate = new DateTime("+7 months");
$cdate->modify("-" . ($cdate->format('j')-1) . " days");
$expiry_date= $cdate->format('Y-m-d');
$expiry_date = strtotime($expiry_date);
which gives the error:
PHP Catchable fatal error:
Object of class DateTime could not be converted to string
it is working with be before ... what is the problem??
DateTime class has no magic method __toString(), so you cannot use this object as string.
You should use getTimestamp()
$cdate = new DateTime("+7 months");
$expiry_date = $cdate->getTimestamp();
$cdate = new DateTime(+7 months);
$cdate = $cdate->format('Y-m-d');
Will cause cdate to be a string if that's what you are going for.