RageQuitKitty
RageQuitKitty

Reputation: 31

Formatting DateTime with a Timezone

I'm having some trouble getting time-zones to play nice on a client project.

I'm pretty sure I'm just too burned out to see what's right in front of my face...

I've got this line...

new DateTime(date('m/d/Y h:i a',$expDate), $timeZone);

I'm trying to get the date and time from this line of code to display on the website (or be used for other date calculations... like whether or not that date/time has passed, etc.) but I want it to use the timezone I specified...

I can do this:

date('M j, Y h:i a T',$expDate)

But it uses UTC for the timezone... which is why I built the DateTime() bit in the first place...


The default value for $timeZone is for EST ('America/New_York') since that's the time-zone my client lives in.

But they have listings for clients all over the US and Canada, so they have an option to set the time-zone to match their clients' location.

I just don't know how to make it actually use that time-zone when it's determining whether or not the expiration date (and time) has passed...

I need it to default to midnight on the expiration date in their client's time zone.

Any suggestions would be greatly appreciated.

Upvotes: 0

Views: 38

Answers (1)

Alireza Fallah
Alireza Fallah

Reputation: 4607

Use this for timezone formatting:

$timezone = new DateTimeZone('America/New_York');
$today = new DateTime("now", $timezone);
$today_formatted = $today->format('M j, Y h:i a T');

Getting clients timezone must be done by JS and here is an answer that certainly will help you with that:

How to detect user's timezone

Upvotes: 1

Related Questions