Skip to main content
added 480 characters in body
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

I can see nothing unexpected. Midnight in UTC is 8 PM the day before in New York, at least in the summer time.

Timestamps are always UTC. This is the reason why your second attempt

$from = DrupalDateTime::createFromFormat('U', strtotime('last week monday'))->getTimestamp();

can be reduced to

$from = strtotime('last week monday');

without changing the result.

Original question

How can I get it to return 2022-06-13 00:00:00 America/New_York instead?

In this case don't convert to UTC timestamps. You can specify the timezone (the Drupal enhanced DrupalDateTime object accepts the timezone as string) when creating the object:

$from = (new DrupalDateTime('last week monday', 'America/New_York'))->render();

results in

"2022-06-13 00:00:00 America/New_York"

Which will internally use a different timestamp than before, but this is only relevant if you convert it to a different timezone, for example to store it in the database or use it in a query against stored values.

Updated OP in the comments

The goal is to query for nodes created Monday to Monday, which only happens in this timezone in EDT.

The Created field in nodes is a timestamp field, so in this case you need the UTC timestamp:

$from = (new DrupalDateTime('last week monday', 'America/New_York'))->getTimestamp();

As you have already noticed in the second attempt, using legacy PHP date_default_timezone_set()/strtotime(), this will be indeed 4 hours ahead of New York time.

I can see nothing unexpected. Midnight in UTC is 8 PM the day before in New York, at least in the summer time.

Timestamps are always UTC. This is the reason why your second attempt

$from = DrupalDateTime::createFromFormat('U', strtotime('last week monday'))->getTimestamp();

can be reduced to

$from = strtotime('last week monday');

without changing the result.

Original question

How can I get it to return 2022-06-13 00:00:00 America/New_York instead?

In this case don't convert to UTC timestamps. You can specify the timezone (the Drupal enhanced DrupalDateTime object accepts the timezone as string) when creating the object:

$from = (new DrupalDateTime('last week monday', 'America/New_York'))->render();

results in

"2022-06-13 00:00:00 America/New_York"

Which will internally use a different timestamp than before, but this is only relevant if you convert it to a different timezone, for example to store it in the database or use it in a query against stored values.

Updated OP in the comments

The goal is to query for nodes created Monday to Monday, which only happens in this timezone in EDT.

The Created field in nodes is a timestamp field, so in this case you need the UTC timestamp:

$from = (new DrupalDateTime('last week monday', 'America/New_York'))->getTimestamp();

As you have already noticed in the second attempt using legacy PHP date_default_timezone_set()/strtotime() this will be indeed 4 hours ahead of New York time.

I can see nothing unexpected. Midnight in UTC is 8 PM the day before in New York, at least in the summer time.

Timestamps are always UTC. This is the reason why your second attempt

$from = DrupalDateTime::createFromFormat('U', strtotime('last week monday'))->getTimestamp();

can be reduced to

$from = strtotime('last week monday');

without changing the result.

Original question

How can I get it to return 2022-06-13 00:00:00 America/New_York instead?

In this case don't convert to UTC timestamps. You can specify the timezone (the Drupal enhanced DrupalDateTime object accepts the timezone as string) when creating the object:

$from = (new DrupalDateTime('last week monday', 'America/New_York'))->render();

results in

"2022-06-13 00:00:00 America/New_York"

Which will internally use a different timestamp than before, but this is only relevant if you convert it to a different timezone, for example to store it in the database or use it in a query against stored values.

Updated OP in the comments

The goal is to query for nodes created Monday to Monday, which only happens in this timezone in EDT.

The Created field in nodes is a timestamp field, so in this case you need the UTC timestamp:

$from = (new DrupalDateTime('last week monday', 'America/New_York'))->getTimestamp();

As you have already noticed in the second attempt, using legacy PHP date_default_timezone_set()/strtotime(), this will be indeed 4 hours ahead of New York time.

added 480 characters in body
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

I can see nothing unexpected. Midnight in UTC is 8 PM the day before in New York, at least in the summer time.

Timestamps are always UTC. This is the reason why your second attempt

$from = DrupalDateTime::createFromFormat('U', strtotime('last week monday'))->getTimestamp();

can be reduced to

$from = strtotime('last week monday');

without changing the result.

SoOriginal question

How can I get it to return 2022-06-13 00:00:00 America/New_York instead?

In this case don't convert to UTC timestamps. You can specify the timezone (the Drupal enhanced DrupalDateTime object accepts the timezone as string) when creating the DateTime object:

$from = (new DrupalDateTime('last week monday', 'America/New_York'))->render();

results in

"2022-06-13 00:00:00 America/New_York"

Which will internally use a different timestamp than before, but this is only relevant if you convert it to a different timezone, for example to store it in the database or use it in a query against stored values.

Updated OP in the comments

The goal is to query for nodes created Monday to Monday, which only happens in this timezone in EDT.

The Created field in nodes is a timestamp field, so in this case you need the UTC timestamp:

$from = (new DrupalDateTime('last week monday', 'America/New_York'))->getTimestamp();

As you have already noticed in the second attempt using legacy PHP date_default_timezone_set()/strtotime() this will be indeed 4 hours ahead of New York time.

I can see nothing unexpected. Midnight in UTC is 8 PM the day before in New York, at least in the summer time.

Timestamps are always UTC. This is the reason why your second attempt

$from = DrupalDateTime::createFromFormat('U', strtotime('last week monday'))->getTimestamp();

can be reduced to

$from = strtotime('last week monday');

without changing the result.

So don't convert to UTC timestamps. You can specify the timezone when creating the DateTime object:

$from = (new DrupalDateTime('last week monday', 'America/New_York'))->render();

results in

"2022-06-13 00:00:00 America/New_York"

Which will internally use a different timestamp than before, but this is only relevant if you convert it to a different timezone, for example to store it in the database.

Updated OP in the comments

The goal is to query for nodes created Monday to Monday, which only happens in this timezone in EDT.

$from = (new DrupalDateTime('last week monday', 'America/New_York'))->getTimestamp();

I can see nothing unexpected. Midnight in UTC is 8 PM the day before in New York, at least in the summer time.

Timestamps are always UTC. This is the reason why your second attempt

$from = DrupalDateTime::createFromFormat('U', strtotime('last week monday'))->getTimestamp();

can be reduced to

$from = strtotime('last week monday');

without changing the result.

Original question

How can I get it to return 2022-06-13 00:00:00 America/New_York instead?

In this case don't convert to UTC timestamps. You can specify the timezone (the Drupal enhanced DrupalDateTime object accepts the timezone as string) when creating the object:

$from = (new DrupalDateTime('last week monday', 'America/New_York'))->render();

results in

"2022-06-13 00:00:00 America/New_York"

Which will internally use a different timestamp than before, but this is only relevant if you convert it to a different timezone, for example to store it in the database or use it in a query against stored values.

Updated OP in the comments

The goal is to query for nodes created Monday to Monday, which only happens in this timezone in EDT.

The Created field in nodes is a timestamp field, so in this case you need the UTC timestamp:

$from = (new DrupalDateTime('last week monday', 'America/New_York'))->getTimestamp();

As you have already noticed in the second attempt using legacy PHP date_default_timezone_set()/strtotime() this will be indeed 4 hours ahead of New York time.

added 233 characters in body
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

I can see nothing unexpected. Midnight in UTC is 8 PM the day before in New York, at least in the summer time.

Timestamps are always UTC. This is the reason why your second attempt

$from = DrupalDateTime::createFromFormat('U', strtotime('last week monday'))->getTimestamp();

can be reduced to

$from = strtotime('last week monday');

without changing the result.

So don't convert to UTC timestamps. You can specify the timezone when creating the DateTime object:

$from = (new DrupalDateTime('last week monday', 'America/New_York'))->render();

results in

"2022-06-13 00:00:00 America/New_York"

Which will internally use a different timestamp than before, but this is only relevant if you convert it to a different timezone, for example to store it in the database.

Updated OP in the comments

The goal is to query for nodes created Monday to Monday, which only happens in this timezone in EDT.

$from = (new DrupalDateTime('last week monday', 'America/New_York'))->getTimestamp();

I can see nothing unexpected. Midnight in UTC is 8 PM the day before in New York, at least in the summer time.

Timestamps are always UTC. This is the reason why your second attempt

$from = DrupalDateTime::createFromFormat('U', strtotime('last week monday'))->getTimestamp();

can be reduced to

$from = strtotime('last week monday');

without changing the result.

So don't convert to UTC timestamps. You can specify the timezone when creating the DateTime object:

$from = (new DrupalDateTime('last week monday', 'America/New_York'))->render();

results in

"2022-06-13 00:00:00 America/New_York"

Which will internally use a different timestamp than before, but this is only relevant if you convert it to a different timezone, for example to store it in the database.

I can see nothing unexpected. Midnight in UTC is 8 PM the day before in New York, at least in the summer time.

Timestamps are always UTC. This is the reason why your second attempt

$from = DrupalDateTime::createFromFormat('U', strtotime('last week monday'))->getTimestamp();

can be reduced to

$from = strtotime('last week monday');

without changing the result.

So don't convert to UTC timestamps. You can specify the timezone when creating the DateTime object:

$from = (new DrupalDateTime('last week monday', 'America/New_York'))->render();

results in

"2022-06-13 00:00:00 America/New_York"

Which will internally use a different timestamp than before, but this is only relevant if you convert it to a different timezone, for example to store it in the database.

Updated OP in the comments

The goal is to query for nodes created Monday to Monday, which only happens in this timezone in EDT.

$from = (new DrupalDateTime('last week monday', 'America/New_York'))->getTimestamp();
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217
Loading