Jimmy
Jimmy

Reputation: 103

compare date and time with users timezone php

I'm creating an auction site where time and date is inserted in following format:

Auction Date : 08/20/2015 @ 05:30 PM IST

I want to match this time with my user's time and dont want to show auctions which have passed the start date as per users time zone. Assume users may come from different timezones.

Can someone please guide me on this.

Upvotes: 0

Views: 191

Answers (1)

MarkSkayff
MarkSkayff

Reputation: 1384

You would have to storage your user's timezone somewhere for comparisson purposes. This means your user's model (user's specific info in your system) must know about their timezone.

Then in PHP you have the DateTime and DateTimeZone classes to play with it.

To give you an example, and this is in the docs you can instantiate DateTime like this:

$dt = new DateTime("your date string", new DateTimeZone("[timezone_location_string]"));

You can compare DateTime objects with the PHP comparisson operators (<,>,>=,<=,==) which is pretty nice.

And here you have a list of timezones used in PHP for instantiating objects like DateTimeZone():

http://php.net/manual/en/timezones.php

Upvotes: 1

Related Questions