0

I want to reuse DateTime object for new date/time value.
Something like:

$currentDate = new \DateTime( '2015-10-09 11:30:42' ); //in UTC timezone
$currentDate->setTimezone( new \DateTimeZone('America/Los_Angeles') ); //in PST
pr( $currentDate );                           //[date] => 2015-10-09 04:30:42


Now I want to reuse above $currentDate object for new UTC datetime value say '2016-12-30 21:12:13' but convert to same timezone i.e., PST. So output would be 2016-12-30 14:12:13 as UTC is 7 hours ahead of PST.

Is there any way to achieve this.

tejas033
  • 175
  • 1
  • 1
  • 8
  • Probably duplicate: http://stackoverflow.com/questions/2505681/timezone-conversion-in-php – Avishake Jul 26 '16 at 10:44
  • Thanks @Avishake, I checked above link, but that's not what I'm searching for. I want to reuse DateTime object for same Timezone which is set earlier. Something like `$currentDate->setNewDateTime('2016-12-30 21:12:13')` so this will convert provided UTC datetime to PST. So does PHP provides such standard function or do I need to create one such function and use it? – tejas033 Jul 26 '16 at 11:26
  • 1
    Can you not just create a new object and set the timezone as you have already done? – Colonel Mustard Jul 26 '16 at 12:05
  • You can create n numbers of object and use those object. In one object you can keep the UTC date time and in another one keep PST date time. That will be easy I think. – Avishake Jul 26 '16 at 12:58
  • From the comments you should see that your question is not overly good, as it seems more about your solution, rather than about your actual problem. You may want to read about [**the XY problem**](http://meta.stackexchange.com/a/66378) and update your question with an explanation of _why_ you actually try to reuse a date object instead of creating a new one. – ndm Jul 26 '16 at 13:44

0 Answers0