0
votes

I am trying convert a utc time stored date to another time zone but i cant seem to get it right. I have a time :

   date1 = new DateTime('first day of the month'); 
   date1.setTime(0,0,0); // Since using the first day of the month seems return the current time with different date

The default DateTime timezone is in UTC. The time i want to make reference is in 'Europe/Amsterdam' timezone. Any way i cant get the time in 'Europe/Amsterdam' timezone to be equivalent to the first day of the month time in UTC? (Uh, sorry my question was confusing.. let me just give an example to be clear). Im trying to query from a db.

   If UTC date time is June 01, 2013. 00:00:00
   I want to get get May 29, 2013 19:55:00.

I tried getting the difference between the two declared times with different timezones to get the time that i wanted but it seems it didnt work :( My Edit/ Clarification:

If use this code:

   $date1 = new DateTime('first day of the month'); 
   $date1.setTime(0,0,0);
   print_r($date1->format('Y-m-d H:i:s'));

I would get:

   2013-06-01 00:00:00

Then if i use timezone:

   $date1->setTimeZone(new DateTimeZone('Europe/Amsterdame'));
   print_r($date1->format('Y-m-d H:i:s'));

I would get: (This is just a sample output):

   2013-06-01 03:00:00

Because of time difference. Want i want to get is like the reverse: I want to get the datetime that when converted 'UTC' timezone i would get this: 06-01-2013 00:00:00 time. So my preffered output is : 2013-05-29 21:00:00 ...

2
That's not valid PHP.Amal Murali
I don't see how June 1st, 00:00 UTC relates to May 29, 19:55 Amsterdam at all.deceze
@AmalMurali, uh i know its not valid. Its a PSUDOCODE.. =_=Jane Deuschandell
@DamienOvereem Yes. I know how to use setTimezone.. I guess my question really isnt clear.. lemme just edit it..Jane Deuschandell

2 Answers

2
votes

You can do in an OOP way like so.

$date = new DateTime('2000-01-01 00:00:00', new DateTimeZone('Europe/Amsterdam'));
echo $date->format('Y-m-d H:i:s P') . "\n";
0
votes

To set the default date in PHP, you can either set it in your ini file or in a PHP file like so:

date_default_timezone_set('Europe/Amsterdam');

Then to format the date, refer to http://www.php.net/manual/en/function.date.php for formatting.

In your case this would be:

date('j M Y' time());

Where j = day, M = month and Y = year.