简体   繁体   English

与用户时区php比较日期和时间

[英]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 拍卖日期:IST 08/20/2015 @ 05:30

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. 有人可以指导我吗?

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. 然后在PHP中,您可以使用DateTime和DateTimeZone类来玩它。

To give you an example, and this is in the docs you can instantiate DateTime like this: 举一个例子,在文档中,您可以像这样实例化DateTime:

$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. 您可以将DateTime对象与PHP比较运算符(<,>,> =,<=,==)进行比较,这非常不错。

And here you have a list of timezones used in PHP for instantiating objects like DateTimeZone(): 这是PHP中用于实例化诸如DateTimeZone()之类的对象的时区列表:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM