60

How do you create a DateTime from timestamp in versions less than < 5.3?

In 5.3 it would be:

$date = DateTime::createFromFormat('U', $timeStamp);

The DateTime constructor wants a string, but this didn't work for me

$date = new DateTime("@$timeStamp");
6
  • 1
    According to the manual, that should work. Have you tried $date = new DateTime('@' . $timeStamp); ? And by "didn't work", what do you mean? Commented Dec 1, 2010 at 22:45
  • 1
    Define didn't work for me. Errors? Wrong date/time? Commented Dec 1, 2010 at 22:46
  • Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() [<a href='datetime.--construct'>datetime.--construct</a>]: Failed to parse time string (@) at position 0 (@): Unexpected character' Commented Dec 1, 2010 at 22:55
  • 6
    @Yarin: It's giving you that error because the timestamp is empty. Find out why. Commented Dec 1, 2010 at 23:15
  • 1
    What does var_dump($timeStamp) give you? Commented Dec 2, 2010 at 8:51

4 Answers 4

46

PHP 5 >= 5.3.0

$date = new DateTime();
$date->setTimestamp($timeStamp);

Edit: Added correct PHP version for setTimestamp

Sign up to request clarification or add additional context in comments.

2 Comments

Think this is PHP 5 >= 5.3
This answer is incorect, setTimestamp() added in php 5.3
32

Assuming you want the date and the time and not just the date as in the previous answer:

$dtStr = date("c", $timeStamp);
$date = new DateTime($dtStr);

Seems pretty silly to have to do that though.

1 Comment

This solution works, but unnecessarily spends time formatting the date string, then re-parsing.
15

It's not working because your $timeStamp variable is empty. Try echoing the value of $timeStamp right before creating the DateTime and you'll see. If you run this:

new DateTime('@2345234');

You don't get an error. However, if you run:

new DateTime('@');

It produces the exact error you said it gives you. You'll need to do some debugging and find out why $timeStamp is empty.

2 Comments

Be warned, that using @ in the constructor will ignore the environment's current timezone and the timezone when supplied in the second argument., even when using $dateTime->modify('@' . 1234) use setTimezone(date_default_timezone_get()) to change from the UTC timezone to the environment timezone.
I had this same problem when I used '@' with an empty timestamp, thanks for this answer. The error message PHP gave was less than helpful, it implied the '@' was the problem.
3

The following works:

$dateString = date('Ymd', $timeStamp);
$date = new DateTime($dateString);

2 Comments

Timestamp[1] is not just Ymd, it's number of seconds(micro seconds in some cases) since 1st Jan 1970 UTC. [1] = en.wikipedia.org/wiki/Unix_time
May be $dateString = date('U', $timeStamp); then?!

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.