2

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.

5
  • 2
    Where does that timestamp come from? Currently, the timestamp starts with 16, and the one you gave is 31 years from the future Commented Jun 30, 2021 at 11:15
  • 1
    Also, how is this related to Symfony? Please either explain the relation, or remove the irrelevant tags Commented Jun 30, 2021 at 11:16
  • Why do you have\ before DataTime ? And I think DateTime consructor ask for now or the format date, not an int. Source: php.net/manual/fr/datetime.construct.php Commented Jun 30, 2021 at 11:16
  • DateTime except an actual date for example '2021-06-30' not unix timestamp. You could instantiate the DateTime class then use ->setTimestamp function on it. Commented Jun 30, 2021 at 11:17
  • @Elikill58 - I expect the \DateTime is because the posted source code is declared inside a namespace. Commented Jun 30, 2021 at 11:22

3 Answers 3

2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you but the given answer gives "DateTime::__construct() expects parameter 1 to be string, object given @kopz
The code in the answer does not pass any parameter to DateTime ...
1

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.

Comments

0

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

3 Comments

Thank you but the given answer gives "DateTime::__construct() expects parameter 1 to be string, object given @АлександрЧерножуков
@pohofo You must pass a string like "@2602585961" in your method setStartTime
@pohofo for test do it $newEntityObject->setStartTime("@2602585961");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.