I am pulling DateTime timestamp result from another table which is set as:
When dumping specific value of $post->getUploadTime() I get:
"2602585961"
It's in string format.
In my entity I have modified the setter to:
public function setStartTime($startTime)
{
$date = new \DateTime($startTime);
$this->startTime = $date->getTimestamp();
return $this;
}
And my code:
$newEntityObject->setStartTime(intval($post->getUploadTime()));
I am using intval() to transform string to integer (timestamp) so I can insert it in db but I get an error:
DateTime::__construct(): Failed to parse time string (2602585961) at position 8 (6): Unexpected character"
It's an error with or without the intval().
I can not figure out what is wrong?
I know there are a lot of posts about the issue. I tried them, but the problem still remains.
Try :
$date = new \DateTime();
$date->setTimestamp($startTime);
$this->startTime = $date->getTimestamp();
But since you are trying to assign a timestamp to your startTime property and you are already passing a timestamp to your function you can just assign whatever timestamp you are passing:
$this->startTime = $startTime;
You have a timestamp, and you are trying to make it a DateTime and get timestamp from the new datetime object.
The DateTime constructor only accept specific date and time format.
Also, the given value refer to the year 2052. So it's possible that you have another issue before.
You don't need to convert to a number. Because you need a string argument that starts with #
You need use #. Read Documentation.
<?php
$a = "#2602585961";
function setStartTime($startTime)
{
$date = new \DateTime($startTime);
$b = $date->getTimestamp();
return $b;
}
echo setStartTime($a);
https://www.php.net/manual/ru/datetime.formats.compound.php
In your code:
$newEntityObject->setStartTime("#" . $post->getUploadTime());
Related
i am trying to find difference between two dates in php, the first date is coming from my database (which is in datetime format), so i am converting it to date and then finding the difference using the current date. my code is like below:
$ret=mysqli_query($con,"select * from members");
$cnt=1;
while ($row=mysqli_fetch_array($ret))
{
$date_time = $row['date'];
$new_date = date("Y-m-d",strtotime($date_time));
$dates = date('Y-m-d');
$diff = $new_date->diff($dates);
echo $diff;
}
then i am getting the following error:
Uncaught Error: Call to a member function diff() on string in C:\xampp\htdocs\form\admin\members.php:123 Stack trace: #0 {main} thrown in C:\xampp\htdocs\form\admin\members.php on line 123
can anyone please tell me what i am doing here is wrong. thanks in advance
date function returns a string.
https://www.php.net/manual/en/function.date.php
You can use date_diff function to compare dates in string format
https://www.php.net/manual/en/function.date-diff.php
or use DateTime class which has method diff.
https://www.php.net/manual/en/class.datetime.php
Use date_diff function instead of the object notation here, DateTime object if you want your date to be Object
date() function returns a string. You need to create an object of DateTime class. Your loop should look something like this:
foreach ($ret as $row) {
$new_date = new \DateTime($row['date']);
$diff = $new_date->diff(new \DateTime());
var_dump($diff);
}
I'm very new using Laravel and I have a problem with a column that I created in the migrations. I have a column that is type date. My problem is that I don't know how can I manage it, how do I have to store some data for a form, etc.
I was asking if someone could tell me, in a method of a controller with the parameter Resquest $r, how can a convert the string from that form in a variable of date. I've been searching and I can't find anything that could help me. Thanks in advance!
Edit:
This is one method of my controller:
public function addObjectives(Request $r,$id){
$gobjectives = Mgobjective::all();
$sobjectives = Msobjective::all();
$mprogram = Mprogram::find($id);
$a = Area::find($mprogram->marea_id);
if($a==null){
$area = new Area();
$area->id = $mprogram->marea_id;
$area->saveArea();
}
$p = Program::find($id);
if($p==null){
$program = new Program();
$program->id = $id;
$program->initialDate= "";
$program->finalDate= "";
$program->frequency= "";
$program->area_id = $mprogram->marea_id;
$program->saveProgram();
}
}
In initialDate y finalDate de program, I have an empty string because before it was a string. I would like to put there a date that doesn't have to be the actual date.
Use date() function like:
$date = date('Y-m-d', strtotime('your date'));
Explanation: Y-m-d is the default date format for mysql date type. So what ever formated date you have, convert it into Y-m-d.
<?php
namespace App\Http\Controllers;
class YourController extends Controller
{
public function yourMethod(Request $request)
{
// If you use a HTML date element, you "should" receive the data in yyyy-mm-dd
// format which can be understood by DateTime's constructor
$date = new \DateTime($request->input('date'));
// If you use a different input or expect a date in a different format you can
// use the createFromFormat method, where your first parameter is the date
// format you expect and the second is the date input.
$date = \DateTime::createFromFormat('d/m/Y', $request->input('date'));
// Once your date has been loaded into a DateTime object, you can use the
// format method to output the date in any format you like. MySQL and other
// databases tend to expect and store dates in yyyy-mm-dd format.
echo $date->format('Y-m-d');
}
}
Try using Carbon
Carbon::createFromDate($year, $month, $day, $tz);
or
Carbon::createFromFormat('Y-m-d H', '1975-05-21 22')->toDateTimeString(); // 1975-05-21 22:00:00
Take a look at the documentation
In my table, I have a variable $duration which is stored in minutes.
And I also have a time variable. Let it be $time1.
$time1=date('H:i:s',$time);
$duration=1000; //minutes
$time2= secondsToTime($duration*60);
I convert the $duration to time format using the function given below.
function secondsToTime($seconds) {
$dtF = new DateTime("#0");
$dtT = new DateTime("#$seconds");
return $dtF->diff($dtT)->format('%h:%i:%s');
}
So in $time2, i have something like this stored 11:12:13
And in $time1 i have something like this stored 01:10:19
I want to perform $total=$time1+$time2;
So, I converted $time2 into time format.
$timeform= new DateTime($time2);
$newtime2= $timeform->format('H:i:s');
Now, I add $total=$time1+$newtime2;
But echo date('H:i:s',$total);gave me following error:
Notice: Object of class DateTime could not be converted to int
The second argument of date() should be a timestamp (i.e. an integer, i.e. seconds), not a formatted date string.
As far as I know only comparison operators work on datetime objects ($date1 > $date2), not math operators ($date1 + $date2).
See also http://nl3.php.net/manual/en/datetime.add.php and http://nl3.php.net/manual/en/datetime.modify.php
Use 1 datetime instance for calculating/formatting the total amount of time
Or, convert 2 datetime instances to seconds, add them, and format using date()
The second parameter of date() is expected to be a Unix timestamp as integer, not a DateTime object. (see php.net)
You need to convert your DateTime object into a Unix timestamp.
Try getTimestamp():
echo date('H:i:s',$total->getTimestamp());
I had this construction error when trying to creating a new DateTime object using a timestamp:
Exception: DateTime::_construct(): Failed to parse time string (1372622987) at position 8 (8): Unexpected character in DateTime->_construct()
The object creation code is:
$start_date = new DateTime( "#{$dbResult->db_timestamp}" );
Where $dbResult->db_timestamp is a valid unix timestamp taken from a database. The timestamp in question was:
1372622987
I would understand this error for invalid formats being passed, but this is a genuine timestamp.
The reason this is very strange: I since ran a script to create a new DateTime object with the timestamp passed in as a hard coded value, and it reported no errors.
This seems to have been a one off, but I need an explanation if there is one, as I can't afford for this to happen again.
You should use setTimestamp instead, if you hardcode it:
$start_date = new DateTime();
$start_date->setTimestamp(1372622987);
in your case
$start_date = new DateTime();
$start_date->setTimestamp($dbResult->db_timestamp);
Use the createFromFormat method:
$start_date = DateTime::createFromFormat("U", $dbResult->db_timestamp);
UPDATE
I now recommend the use of Carbon
change your code to this
$start_date = new DateTime( "#" . $dbResult->db_timestamp );
and it will work fine
This worked for me.
/**
* return date in specific format, given a timestamp.
*
* #param timestamp $datetime
* #return string
*/
public static function showDateString($timestamp)
{
if ($timestamp !== NULL) {
$date = new DateTime();
$date->setTimestamp(intval($timestamp));
return $date->format("d-m-Y");
}
return '';
}
$start_date = new DateTime();
$start_date->setTimestamp($dbResult->db_timestamp);
function is_due($due_date)
{
$now=new DateTime('now');
$dnow=$now->format('Y-m-d');
$due=$due_date->format('Y-m-d');
$interval =(strtotime($dnow)-strtotime($due));
print_r($due_date->format('Y-m-d'));
return $interval;
}
i use that function to check if the given date is due
The $due_date is passed in after being retrieved from an external file as a string '2012-12-12' which is assigned as an array element, for example,
$datetime['due']=ReadExtFile();
When I call that function as print_r(is_due($datetime['due'])); or
$due=new DateTime($datetime['due']);
nothing seems to work, I see no output. But print_r($datetime) will display the result of [due].
The way this method is written, it expects $due_date to be a DateTime object, and not a string. You indicate you are passing in a string and not an object.
It looks like you just want to return $interval as a count of from the date given until now. I would suggest just using a simple method that only deals with strings.
function is_due($due_date) {
return time() - strtotime($due_date);
// return ((time() - strtotime($due_date)) >= 0); // depending on function you want this might work
}