Link to home
Start Free TrialLog in
Avatar of egoselfaxis
egoselfaxis

asked on

PHP - converting a date/time value to UTC

How would I convert the following date/time object to UTC?

$opening_time = date('Y-m-d H:i:s', time());

- Yvan
Avatar of ycTIN
ycTIN
Flag of Hong Kong image

echo $opening_time = date(DATE_RFC822);

DATE_ATOM (string)
Atom (example: 2005-08-15T15:52:01+00:00) 
 
DATE_COOKIE (string)
HTTP Cookies (example: Monday, 15-Aug-05 15:52:01 UTC) 
 
DATE_ISO8601 (string)
ISO-8601 (example: 2005-08-15T15:52:01+0000) 
 
DATE_RFC822 (string)
RFC 822 (example: Mon, 15 Aug 05 15:52:01 +0000) 
 
DATE_RFC850 (string)
RFC 850 (example: Monday, 15-Aug-05 15:52:01 UTC) 
 
DATE_RFC1036 (string)
RFC 1036 (example: Mon, 15 Aug 05 15:52:01 +0000) 
 
DATE_RFC1123 (string)
RFC 1123 (example: Mon, 15 Aug 2005 15:52:01 +0000) 
 
DATE_RFC2822 (string)
RFC 2822 (Mon, 15 Aug 2005 15:52:01 +0000) 
 
DATE_RFC3339 (string)
Same as DATE_ATOM (since PHP 5.1.3) 
 
DATE_RSS (string)
RSS (Mon, 15 Aug 2005 15:52:01 +0000) 
 
DATE_W3C (string)
World Wide Web Consortium (example: 2005-08-15T15:52:01+00:00) 

Open in new window

Something like this?
$opening_time = date('Y-m-d H:i:s', strtotime($opening_time)-date("Z"));

Open in new window

Avatar of egoselfaxis
egoselfaxis

ASKER

>> echo $opening_time = date(DATE_RFC822);

This generates the incorrect date/time object for me.

When I ran the script, . it generated the following:

Wed, 20 May 09 10:28:43 -0600

However, the UTC time shoud have actually been 04:28 pm.

I'm currently in EST time zone, .. and it appears that the UTC time offset is merely 4 hrs.

How can I adapt my existing date/time object by adding 4 hrs to the time?

$opening_time = date('Y-m-d H:i:s', time());

 I'm aware of how to do this with the date, .. though I'm not sure if it's possbile to do this with times (and if it intelligentlly increments the date +1 when necessary, etc). Please advise.

- Yvan



 
>> $opening_time = date('Y-m-d H:i:s', strtotime($opening_time)-date("Z"));

This renders the date as "1969-12-31 23:00:00 " -- which is also incorrect
i show how to convert existing $opening_time to UTC ;)
Try this:
$opening_time = date('Y-m-d H:i:s', time()-date("Z")) ;).
ASKER CERTIFIED SOLUTION
Avatar of ycTIN
ycTIN
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> date_default_timezone_set("America/New_York");

I set this to .. date_default_timezone_set("UTC"); ... right before my variable assignment, and that seemed to do the trick.  Thanks!

- yg