2

I have a precise date of 1/1/2020 00:00:00 America/Los_Angeles time that I want to convert into a UNIX timestamp. Many answers out there want to set a default timezone using date_default_timezone_set before inserting the string into strtotime() function. But I do not want to mess with the default timezone of the server (which is probably UTC).

Is there anything wrong with doing strtotime("1/1/2020 00:00:00 America/Los_Angeles") to get my timestamp?

I get the correct UNIX value this way. It's just, there's nothing in the docs to indicate this is valid.

2
  • 2
    The docs do cover the date time valid formats here. Commented Nov 20, 2019 at 0:49
  • 1
    @AlexBarker Thank you, this is exactly the information I needed. Commented Nov 20, 2019 at 0:52

2 Answers 2

2

As your time str has time zone, there is no need to set the default time zone. But when you want to print it, you need to set default timezone.

php > echo date_default_timezone_get();
UTC

php > echo date("Y-m-d H:i:s",strtotime("1/1/2020 00:00:00 America/Los_Angeles"));
2020-01-01 08:00:00

php > echo date("Y-m-d H:i:s",strtotime("1/1/2020 00:00:00"));
2020-01-01 00:00:00

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

3 Comments

This is a very good answer. I just wanted to add that the built-in DateTime class can also convert timezones for you without messing around with the default timezone. This could be useful if you need to get the timestamp back in the America/Los_Angeles timezone.
Thank you for the answer. Is there any specific list of strings allowed here? Could I have said PST instead of America/Los_Angeles to get the same answer? Docs just says "...will try to parse that format into a Unix timestamp". Will try doesn't work for me. Edit: just saw Alex Barker's comment that directs to the docs.
@NamKim I added the link to a comment in your main question. I presume any valid TZ abbreviation know by the underlying system should be fine.
1

strtotime and datetime also process time zone information located in the date string. If omitted, the local settings of the server are used.

This provides a correct timestamp

strtotime("1/1/2020 00:00:00 America/Los_Angeles");

But the output with date() uses the local time zone of the server.

These problems do not exist when the DateTime class is used. In the DateTime object, the time zone is stored in the object, regardless of the server's local settings.

echo date_default_timezone_get(); //my Server get Europe/Berlin

$dateTimeLA = date_create("1/1/2020 00:00:00", new DateTimeZone('America/Los_Angeles'));

echo $dateTimeLA->format("Y-m-d H:i:s e");

Output: 2020-01-01 00:00:00 America/Los_Angeles

If timestamp is needed:

$tsLA = $dateTimeLA->getTimeStamp();
echo $tsLA;  //1577865600

How to create a DateTime-Object from timestamp:

$dateFromTs = date_create(null, new DateTimeZone('America/Los_Angeles'));
$dateFromTs->setTimestamp(1577865600);
var_dump($dateFromTs);

Output:

object(DateTime)#4 (3) {
  ["date"]=>
  string(19) "2020-01-01 00:00:00"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(19) "America/Los_Angeles"
}

Comments

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.