Call to a member function format() on a non-object DateTime - php

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...

Related

How to add time of timeType to DateTimeType

I want to add variable of type TimeType to a variable of DateTimeType
I have triying this code :
$newDate = $dateAppointment->add($duration);
but i got this error
Warning: DateTime::add() expects parameter 1 to be DateInterval,
object given
Examples of data:
$dateAppountment = 2019-03-21 10:15:00
$duration : 00:15:00
I suppose there's some kind of transformer already exists in symfony, but I can't find it. So, here's a custom code how to convert time from DateTime to DateInterval and add it to another \DateTime:
$dateAppointment = (new \DateTime());
$dtDuration = (new \DateTime())->setTime(1, 15, 0);
$duration = $duration->format('\P\TH\Hi\Ms\S');
$newDate = $dateAppointment->add(new \DateInterval($duration));
Fiddle: https://3v4l.org/5lFlQ
Here is an example:
$duration = '1970-01-01 01:00:00.000000';
$timeObject = DateTime::createFromFormat('Y-m-d H:i:s.u', $duration, new DateTimeZone('UTC'));
$date = '2019-03-21 10:15:00';
$dateObject = DateTime::createFromFormat('Y-m-d H:i:s', $date);
$modify = '+' . $timeObject->format('U') . ' second';
echo $dateObject->modify($modify)->format('Y-m-d H:i:s');
Just get the seconds from your duration object and add them to your date object. If you used modify, there are a lot of things you can do.

Not getting the required date

I want to calculate the difference between date using date_diff(), whose 1st parameter is saved data in database and the 2nd parameter is today's date. The $pro_deadline is coming from database and is of type text (format yyyy-mm-dd), so I converted it into time using strtotime(). But in the end I'm getting "
Warning
: date_diff() expects parameter 1 to be DateTimeInterface, string given"
$today = date("Y-m-d");
echo $today;
$end = strtotime($pro_deadline);
$end_line = date("Y-m-d",$end);
echo $end_line;
$diff = date_diff($end_line,$today);
echo $diff;
as per PHP documentation http://php.net/manual/en/function.date-diff.php
date_diff — Alias of DateTime::diff()
so the perameters to date_diff should be DateTimeInterface types.
i would try
<?php
$today = date("Y-m-d");
echo $today." ";
$today = date_create($today);
$pro_deadline = '10-15-18';
$end = strtotime($pro_deadline);
$end_line = date_create(date("Y-m-d",$end));
$diff = date_diff($end_line,$today);
echo $diff->format('%a');
echo " days apart";
?>
the date_create() function is an alias of the DateTime constructor.
http://php.net/manual/en/datetime.construct.php
this creates an interface for the date/time that the date_diff() function can interpret. then date_diff() returns a DateInterval object
http://php.net/manual/en/class.dateinterval.php
the DateInterval object has a format method
http://php.net/manual/en/dateinterval.format.php
that can return the date in a sting for you.
Hope this explanation helps!
Like the error message says, date_diff expects DateTimeInterface parameters. strtotime returns a timestamp as an integer, which it can't work with.
Instead of creating timestamps, you can pass your deadline to the DateTime constructor, along with another version that'll default to now:
$today = new DateTime;
$end = new DateTime($pro_deadline);
and then pass these two objects to date_diff, and use the DateInterval::format method to display the number of days (assuming this is your desired output):
$diff = date_diff($today,$end);
echo $diff->format('%a');
See https://3v4l.org/QVkad for a full example
First of all, if you want a difference between a date in a database and today's date, just do it in the database directly. You didn't specify which DB, but, for example in MySQL you'd do something like:
SELECT DATEDIFF(some_field, now()) FROM ...
If you insist on doing it in PHP, then don't use strtotime but use DateTime object:
$today = new DateTime();
$end = new DateTime($pro_deadline);
$diff = $end.diff($today)
The date() function returns a simple string, but the date_diff() function expects a date object.
You can do it all much more simply with the functions in the DateTime class:
$pro_deadline = "2018-09-01";
$today = new DateTime();
$end = new DateTime($pro_deadline);
$interval = $end->diff($today);
echo $interval->format('%R%a days');
This example outputs +25 days Click here for Runnable Demo
Further examples of the diff() function here

PHP Fatal error when trying to add a DateInterval to a DateTime object

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');

php Object of class DateInterval could not be converted to string

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

Cannot convert DateTime object to string

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.

Categories