PHP Carbon 检查所选日期是否大于其他日期
PHP Carbon Check If Chosen Date is Greater than Other Date
我已经开始在我的应用程序中使用 PHP Carbon,因为它似乎比使用和操作 date/time 和 DateTime class 容易得多。我想要做的是检查所选日期 ($chosen_date) 是否大于另一个日期 ($whitelist_date)。我在下面的代码中试过这个:
$chosen_date = new Carbon($chosen_date);
$whitelist_date = Carbon::now('Europe/London');
$whitelist_date->addMinutes(10);
echo "Chosen date must be after this date: ".$whitelist_date ."</br>";
echo "Chosen Date: ".$chosen_date ."</br>";
if ($chosen_date->gt($whitelist_date)) {
echo "proceed";
} else {
echo "dont proceed";
}
原始$chosen_date值来自POST数据。这是我得到的输出:
Chosen date must be after this date: 2015-09-22 21:21:57
Chosen Date: 2015-09-22 21:01:00
proceed
显然选择的日期不大于白名单日期,但 if 语句 returns 仍然是 true 并且 echo 的 "proceed"。我一遍又一遍地检查代码,但看不出哪里出错了。
或尝试使用以下方法:
if ($chosen_date->gte($whitelist_date))
可能是时区不一样,试试这个
$chosen_date = new Carbon($chosen_date, 'Europe/London');
$whitelist_date = Carbon::now('Europe/London');
$whitelist_date->addMinutes(10);
请记住,您始终可以构建实例并为其设置时区:
$date = new Carbon();
$date->setTimezone('Europe/London');
$whitelist_date = $date->now();
Any tips on how I can manage data for users with different timezones?
您可以创建具有不同时区的不同对象。试试这个并测试结果。
$london_date = new Carbon($chosen_date_from_london, 'Europe/London');
$colombia_date = new Carbon($chosen_date_from_colombia, 'Bogota/America');
假设您将它们进行比较:
$are_different = $london_date->gt($colombia_date);
var_dump($are_different); //FALSE
不,它们并没有什么不同,尽管当你盯着时钟看时它们是不同的时间,在世界的不同地方,它们仍然处于同一个当下,即现在。
好了,只需创建不同的对象或 Carbon() 实例,并使用 $instance->setTimeZone(TimeZone);
设置不同的时区
我已经开始在我的应用程序中使用 PHP Carbon,因为它似乎比使用和操作 date/time 和 DateTime class 容易得多。我想要做的是检查所选日期 ($chosen_date) 是否大于另一个日期 ($whitelist_date)。我在下面的代码中试过这个:
$chosen_date = new Carbon($chosen_date);
$whitelist_date = Carbon::now('Europe/London');
$whitelist_date->addMinutes(10);
echo "Chosen date must be after this date: ".$whitelist_date ."</br>";
echo "Chosen Date: ".$chosen_date ."</br>";
if ($chosen_date->gt($whitelist_date)) {
echo "proceed";
} else {
echo "dont proceed";
}
原始$chosen_date值来自POST数据。这是我得到的输出:
Chosen date must be after this date: 2015-09-22 21:21:57
Chosen Date: 2015-09-22 21:01:00
proceed
显然选择的日期不大于白名单日期,但 if 语句 returns 仍然是 true 并且 echo 的 "proceed"。我一遍又一遍地检查代码,但看不出哪里出错了。
或尝试使用以下方法:
if ($chosen_date->gte($whitelist_date))
可能是时区不一样,试试这个
$chosen_date = new Carbon($chosen_date, 'Europe/London');
$whitelist_date = Carbon::now('Europe/London');
$whitelist_date->addMinutes(10);
请记住,您始终可以构建实例并为其设置时区:
$date = new Carbon();
$date->setTimezone('Europe/London');
$whitelist_date = $date->now();
Any tips on how I can manage data for users with different timezones?
您可以创建具有不同时区的不同对象。试试这个并测试结果。
$london_date = new Carbon($chosen_date_from_london, 'Europe/London');
$colombia_date = new Carbon($chosen_date_from_colombia, 'Bogota/America');
假设您将它们进行比较:
$are_different = $london_date->gt($colombia_date);
var_dump($are_different); //FALSE
不,它们并没有什么不同,尽管当你盯着时钟看时它们是不同的时间,在世界的不同地方,它们仍然处于同一个当下,即现在。
好了,只需创建不同的对象或 Carbon() 实例,并使用 $instance->setTimeZone(TimeZone);