Get DateTime in two fields - 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.

Related

How to make a datetime a bigint?

I want to convert a datetime to big int. For Example. I have an entry in the file that i read in php like normal datetime, "2021/05/20 11:46"(i keep it in a variable named $data) that i want to transform into a bigint like 202105201146 (year+month+day+hour+minute) . I tried to work around and make it working, but I still get the error :
"Argument #2 ($timestamp) must be of type ?int, string given in". >
The line in question is :
$ID= date('Y', $data) . date('m', $data) . date('d', $data). date('G', $data) . date('i', $data). date('s', $data);
You must first convert it into a datetime object, and then format it as "bigint":
$dateAsString = '2021/05/20 11:46';
$datetime = \DateTime::createFromFormat('Y/m/d h:i', $dateAsString);
echo $datetime->format('Ymdhi'); // 202105201146
However, since the error is asking for a timestamp (as int value), it could be better to use the getTimestamp() method of DateTime class, which returns the epoch timestamp:
$dateAsString = '2021/05/20 11:46';
$datetime = \DateTime::createFromFormat('Y/m/d h:i', $dateAsString);
$timestamp = $datetime->getTimestamp(); // 1621511160

php converting string into datetime format

I am having problems converting a string into datetime format in order to store variable into a datetime-type field of a sql table
value of $timestamp:
2019-02-23T08:30:03.77
$datum2 = substr($timestamp,0,19);
$datum2 = str_replace('T',' ', $datum2);
echo $datum2 ."<br>";
--> 2019-02-23 08:30:03 .... echo output looks ok to me
$datum2 = date_format($datum2,'Y-m-d H:i:s');
--> error message
Warning: date_format() expects parameter 1 to be DateTimeInterface
Thank you for any hints
Stefan
Check below code:
$date = '2019-02-23 08:30:03';
$datum2 = date('Y-m-d H:i:s', strtotime($date));
You can convert string date to timestamp and later can change to date format. Hope it helps you.
You have to use date_create function on $timestamp:
$datum2 = date_format(date_create($timestamp),'Y-m-d H:i:s');

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

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

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

PHP SQL Datetime issue

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

Categories