I have a DATETIME field within a SQL table and retrieve the data accordingly - when trying to use the date_diff function however I receive the following message:
Message: date_diff() expects parameter 1 to be DateTime, string given
is there a way to convert the string I have taken from the SQL DB back into a date/time, the field format is as follows:
Y -M -D H -M -S
2014-02-15 14:55:29
You are passing the datetime string to date_diff() but that function expects a DateTime() object. You need to create a DateTime() object with the date first, then use date_diff().
$date1 = new DateTime('2014-02-15 14:55:29');
$date2 = new DateTime();
$interval = $date1->diff($date2);
This should work :
$datetime = DateTime::createFromFormat('Y-m-d H:i:s', '2014-02-15 14:55:29');
Related
I'm reading lines from a *.txt file and get strings (date formats) in this style:
2017-10-19 20:51:54 GMT+08:00
2020-03-31 13:19:31 GMT-08:00
2018-04-10 14:35:17 GMT
With the function DateTime::createFromFormat, I want to convert such lines into a time string. After that, I would like to get the timestamp with getTimestamp();. So, currently my code looks like this ($date is my read line):
$date = DateTime::createFromFormat("Y-m-d H:i:s", $date);
$timeStamp = $date->getTimestamp();
When I try to do this, I get this error message:
Fatal error: Uncaught Error: Call to a member function getTimestamp() on bool in ...
Does anyone have an idea how to solve this problem?
Edit:
Regarding Gordon's comment, I also tried to add the missing parts ("GMT" => e and "+08:00" => P) as well, like this:
$date= DateTime::createFromFormat("Y-m-d H:i:s eP", $date);
You are getting this error because the date provided does not match the format specified.
Add the following line after createFromFormat() call -
var_dump(DateTime::getLastErrors());
The above line will return the error. The error message is - "Trailing data". This is because of "GMT+08:00" in the string.
For processing it properly you should provide the optional third parameter to createFormFormat which is timezone and it expects it to be a DateTimeZone object. So update your createFromFormat function as -
$timezone = new DateTimeZone('GMT+08:00');
$date = "2017-10-19 20:51:54";
$date = DateTime::createFromFormat("Y-m-d H:i:s", $date, $timezone);
I hope this helps. You will have to separate the date from the timezone and process it as mentioned above.
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
Hi there and thank you for the help. I will try to be breef.
I have a SQL table with one col named "duration" -> type Time
I need to get this "duration" and add to the actual DateTime -> date()
Till now I got something like these:
$id_mission = $_POST["id_mission"];
$sql="SELECT duration FROM missions WHERE id_mission='".$id_mission."'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
How do i pick this: $row['duration'] and convert to DateTime Object?
$date = new DateTime();
$get_datetime = $date->format('Y-m-d H:i:s');
$get_datetime->add(new DateInterval($row['duration']));
I got these sql error:
Fatal error: Call to a member function add() on string in C:\wamp64\www\players\actions\insert_mission.php on line 18
This line converts your DateTime object into a string which you are then trying to call the add() method with. Strings don't have this method.
$get_datetime = $date->format('Y-m-d H:i:s');
To add the date from your row don't use the format method.
$date = new DateTime();
$date->add(new DateInterval($row['duration']));
The error message that you are seeing has nothing to do with SQL, it is telling you that your are trying to treat a string like an object. Which doesn't work in PHP.
Getting DateTime
Hi I am not good on programming so don't judge so strong, I want to get date and time from fields.
$date_start = $this->input->post('date_start');
$time_start = $this->input->post('time_start');
$date_end = $this->input->post('date_end');
$time_end = $this->input->post('time_end');
$data['start'] = date_format('U = Y-m-d H:i:s', $date_start.' '.$time_start);
$data['end'] = date_format('U = Y-m-d H:i:s', $date_end.' '.$time_end);
Error
date_format() expects parameter 1 to be DateTimeInterface, string given
Try like this
$data['start'] = date_format(date_create( $date_start.' '.$time_start),"U = Y-m-d H:i:s");
The function date_format expects an object implementing DateTimeInterface as the first argument and the format string as the second argument.
You can create a DateTime object from a string with it's factory method DateTime::createFromFormat also procedurally with date_create_from_format documented on the same page.
I am trying to format a datetime variable with the following code:
$passed_time = $stu_quiz->c_date_time;
$passed_time_string = date_format($passed_time, 'M-d-Y');
For some reason, if I print $passed_time_string, the output is blank, but if I print out $passed_time, I get the date (in the format 2011-06-15 21:43:09).
Why is the date_format method not working?
The date_format function expects a "DateTime" object that is created using date_create.
Example:
$passed_time = date_create($stu_quiz->c_date_time);
$passed_time_string = date_format($passed_time, 'M-d-Y');
You are looking for just date()
http://php.net/manual/en/function.date.php
If you don't have PHP 5.3 and cannot use the DateTime class, try this:
$passed_time_string = date("M-d-Y", strtotime($passed_time));
First it converts your original MySQL time to a unix timestamp, then formats it as M-d-Y