2

When I execute Carbon::now('US/Eastern') I get the following from a var_dump:

object(Carbon\Carbon)[559]
  public 'date' => string '2014-12-15 21:52:54.000000' (length=26)
  public 'timezone_type' => int 3
  public 'timezone' => string 'US/Eastern' (length=10)

This is what I am expecting.

When I execute date('Y-m-d H:i:s', Carbon::now('US/Eastern')->timestamp) I get the following:

string '2014-12-16 02:52:54' (length=19)

The timezone is being ignored in the timestamp here.

Why do the date and time differ between each?

How can I get an accurate timestamp into the date function that reflects the timezone?

eComEvo
  • 11,669
  • 26
  • 89
  • 145
  • The US/Eastern timezone is deprecated. Try using America/New_York instead. Docs: http://php.net/manual/en/timezones.others.php – akanevsky Dec 16 '14 at 03:10
  • Are you sure that the timestmap isn't returning something other than an integer, or is not returning false? Because if not, then the 2nd argument will be ignored and `time()` will be used. – Ohgodwhy Dec 16 '14 at 03:15
  • @Ohgodwhy It is returning a timestamp integer. – eComEvo Dec 16 '14 at 03:17
  • Can you take the timestamp integer it provides and hard code it into the 2nd argument and see if it produces the same? – Ohgodwhy Dec 16 '14 at 03:18
  • @akanevsky Using "America/New_York" did not change anything. – eComEvo Dec 16 '14 at 03:18
  • @Ohgodwhy Yep, when I drop in `1418699862` I get the exact same result. – eComEvo Dec 16 '14 at 03:19
  • I'm extremely confused. can you please use `date_default_timezone_set('America/New_York');` before the `date()` call and see if it has any effect? – Ohgodwhy Dec 16 '14 at 03:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/66954/discussion-between-ohgodwhy-and-ecomevo). – Ohgodwhy Dec 16 '14 at 03:23
  • @Ohgodwhy After doing `date_default_timezone_set('America/New_York');` the date/time is correct. However, I have to change the timezone multiple times throughout the code, so having to change the default timezone each time is clunky. – eComEvo Dec 16 '14 at 03:46
  • @eComEvo, instead of setting default timezone in each script, you could just set it in a configuration file and then include that configuration file in each script. If you need to shift between different timezones in one script, then resetting default timezone can lead to bugs. Maybe you could instead use UTC/GMT, and then make a custom function or method that will convert the timestamp from UTC/GMT to proper timezone. Edit: Carbon supports timezone conversion - see http://stackoverflow.com/questions/21607236/get-utc-from-another-timezone-with-carbon – akanevsky Dec 16 '14 at 14:06

0 Answers0